質問編集履歴
2
追加情報
test
CHANGED
File without changes
|
test
CHANGED
@@ -92,6 +92,10 @@
|
|
92
92
|
|
93
93
|
ちなみにRouteServiceProvider.phpのmapApiRoutes関数の->middleware('api')を->middleware('web')に変更しております。参考にしている記事にapiをwebに修正するように載っていたため変更しています。そこが原因でしょうか?
|
94
94
|
|
95
|
+
(記事)
|
96
|
+
|
97
|
+
今回実装する API は内部からしか呼ばれない上にクッキー認証を行うステートフルなものなので、ミドルウェアグループは画面と同じ web に設定します。
|
98
|
+
|
95
99
|
|
96
100
|
|
97
101
|
↓RouteServiceProvider.php
|
1
phpファイルの情報が無かったため必要なファイルのソースを追記しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -86,60 +86,364 @@
|
|
86
86
|
|
87
87
|
コンテナを再起動した時にnginxでエラーが出てしまいました...。
|
88
88
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
89
|
+
|
90
|
+
|
91
|
+
**【ファイルを追記しました】**
|
92
|
+
|
93
|
+
ちなみにRouteServiceProvider.phpのmapApiRoutes関数の->middleware('api')を->middleware('web')に変更しております。参考にしている記事にapiをwebに修正するように載っていたため変更しています。そこが原因でしょうか?
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
↓RouteServiceProvider.php
|
98
|
+
|
99
|
+
```PHP
|
100
|
+
|
101
|
+
<?php
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
namespace App\Providers;
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;
|
110
|
+
|
111
|
+
use Illuminate\Support\Facades\Route;
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
class RouteServiceProvider extends ServiceProvider
|
116
|
+
|
117
|
+
{
|
118
|
+
|
119
|
+
/**
|
120
|
+
|
121
|
+
* This namespace is applied to your controller routes.
|
122
|
+
|
123
|
+
*
|
124
|
+
|
125
|
+
* In addition, it is set as the URL generator's root namespace.
|
126
|
+
|
127
|
+
*
|
128
|
+
|
129
|
+
* @var string
|
130
|
+
|
131
|
+
*/
|
132
|
+
|
133
|
+
protected $namespace = 'App\Http\Controllers';
|
134
|
+
|
135
|
+
|
136
|
+
|
137
|
+
/**
|
138
|
+
|
139
|
+
* Define your route model bindings, pattern filters, etc.
|
140
|
+
|
141
|
+
*
|
142
|
+
|
143
|
+
* @return void
|
144
|
+
|
145
|
+
*/
|
146
|
+
|
147
|
+
public function boot()
|
148
|
+
|
149
|
+
{
|
150
|
+
|
151
|
+
//
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
parent::boot();
|
156
|
+
|
157
|
+
}
|
158
|
+
|
159
|
+
|
160
|
+
|
161
|
+
/**
|
162
|
+
|
163
|
+
* Define the routes for the application.
|
164
|
+
|
165
|
+
*
|
166
|
+
|
167
|
+
* @return void
|
168
|
+
|
169
|
+
*/
|
170
|
+
|
171
|
+
public function map()
|
172
|
+
|
173
|
+
{
|
174
|
+
|
175
|
+
$this->mapApiRoutes();
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
$this->mapWebRoutes();
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
//
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
/**
|
190
|
+
|
191
|
+
* Define the "web" routes for the application.
|
192
|
+
|
193
|
+
*
|
194
|
+
|
195
|
+
* These routes all receive session state, CSRF protection, etc.
|
196
|
+
|
197
|
+
*
|
198
|
+
|
199
|
+
* @return void
|
200
|
+
|
201
|
+
*/
|
202
|
+
|
203
|
+
protected function mapWebRoutes()
|
204
|
+
|
205
|
+
{
|
206
|
+
|
207
|
+
Route::middleware('web')
|
208
|
+
|
209
|
+
->namespace($this->namespace)
|
210
|
+
|
211
|
+
->group(base_path('routes/web.php'));
|
212
|
+
|
213
|
+
}
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
/**
|
218
|
+
|
219
|
+
* Define the "api" routes for the application.
|
220
|
+
|
221
|
+
*
|
222
|
+
|
223
|
+
* These routes are typically stateless.
|
224
|
+
|
225
|
+
*
|
226
|
+
|
227
|
+
* @return void
|
228
|
+
|
229
|
+
*/
|
230
|
+
|
231
|
+
protected function mapApiRoutes()
|
232
|
+
|
233
|
+
{
|
234
|
+
|
235
|
+
Route::prefix('api')
|
236
|
+
|
237
|
+
->middleware('web')
|
238
|
+
|
239
|
+
->namespace($this->namespace)
|
240
|
+
|
241
|
+
->group(base_path('routes/api.php'));
|
242
|
+
|
243
|
+
}
|
244
|
+
|
245
|
+
}
|
140
246
|
|
141
247
|
```
|
142
248
|
|
249
|
+
↓api.php
|
250
|
+
|
251
|
+
```PHP
|
252
|
+
|
253
|
+
<?php
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
use Illuminate\Http\Request;
|
258
|
+
|
259
|
+
|
260
|
+
|
261
|
+
/*
|
262
|
+
|
263
|
+
|--------------------------------------------------------------------------
|
264
|
+
|
265
|
+
| API Routes
|
266
|
+
|
267
|
+
|--------------------------------------------------------------------------
|
268
|
+
|
269
|
+
|
|
270
|
+
|
271
|
+
| Here is where you can register API routes for your application. These
|
272
|
+
|
273
|
+
| routes are loaded by the RouteServiceProvider within a group which
|
274
|
+
|
275
|
+
| is assigned the "api" middleware group. Enjoy building your API!
|
276
|
+
|
277
|
+
|
|
278
|
+
|
279
|
+
*/
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
// Route::middleware('auth:api')->get('/user', function (Request $request) {
|
284
|
+
|
285
|
+
// return $request->user();
|
286
|
+
|
287
|
+
// });
|
288
|
+
|
289
|
+
|
290
|
+
|
291
|
+
//会員登録
|
292
|
+
|
293
|
+
Route::post('/register', 'Auth\RegisterController@register')->name('register');
|
294
|
+
|
295
|
+
//ログイン
|
296
|
+
|
297
|
+
Route::post('/logout', 'Auth\LoginController@logout')->name('logout');
|
298
|
+
|
299
|
+
//ログアウト
|
300
|
+
|
301
|
+
Route::post('/login', 'Auth\LoginController@login')->name('login');
|
302
|
+
|
303
|
+
```
|
304
|
+
|
305
|
+
|
306
|
+
|
307
|
+
↓LoginController.php
|
308
|
+
|
309
|
+
```PHP
|
310
|
+
|
311
|
+
<?php
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
namespace App\Http\Controllers\Auth;
|
316
|
+
|
317
|
+
|
318
|
+
|
319
|
+
use App\Http\Controllers\Controller;
|
320
|
+
|
321
|
+
use Illuminate\Foundation\Auth\AuthenticatesUsers;
|
322
|
+
|
323
|
+
use Illuminate\Http\Request;
|
324
|
+
|
325
|
+
use App\Models\Account;
|
326
|
+
|
327
|
+
|
328
|
+
|
329
|
+
class LoginController extends Controller
|
330
|
+
|
331
|
+
{
|
332
|
+
|
333
|
+
/*
|
334
|
+
|
335
|
+
|--------------------------------------------------------------------------
|
336
|
+
|
337
|
+
| Login Controller
|
338
|
+
|
339
|
+
|--------------------------------------------------------------------------
|
340
|
+
|
341
|
+
|
|
342
|
+
|
343
|
+
| This controller handles authenticating users for the application and
|
344
|
+
|
345
|
+
| redirecting them to your home screen. The controller uses a trait
|
346
|
+
|
347
|
+
| to conveniently provide its functionality to your applications.
|
348
|
+
|
349
|
+
|
|
350
|
+
|
351
|
+
*/
|
352
|
+
|
353
|
+
|
354
|
+
|
355
|
+
use AuthenticatesUsers;
|
356
|
+
|
357
|
+
|
358
|
+
|
359
|
+
/**
|
360
|
+
|
361
|
+
* Where to redirect users after login.
|
362
|
+
|
363
|
+
*
|
364
|
+
|
365
|
+
* @var string
|
366
|
+
|
367
|
+
*/
|
368
|
+
|
369
|
+
protected $redirectTo = '/home';
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
/**
|
374
|
+
|
375
|
+
* Create a new controller instance.
|
376
|
+
|
377
|
+
*
|
378
|
+
|
379
|
+
* @return void
|
380
|
+
|
381
|
+
*/
|
382
|
+
|
383
|
+
public function __construct()
|
384
|
+
|
385
|
+
{
|
386
|
+
|
387
|
+
$this->middleware('guest')->except('logout');
|
388
|
+
|
389
|
+
}
|
390
|
+
|
391
|
+
|
392
|
+
|
393
|
+
protected function authenticated(Request $request, $user, Account $account)
|
394
|
+
|
395
|
+
{
|
396
|
+
|
397
|
+
$serch = $request->login_id;
|
398
|
+
|
399
|
+
$getUser = $account::where('login_id', $serch)->first();
|
400
|
+
|
401
|
+
return $getUser;
|
402
|
+
|
403
|
+
}
|
404
|
+
|
405
|
+
|
406
|
+
|
407
|
+
protected function loggedOut(Request $request)
|
408
|
+
|
409
|
+
{
|
410
|
+
|
411
|
+
// セッションを再生成する(セッションID固定化攻撃対策)
|
412
|
+
|
413
|
+
$request->session()->regenerate();
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
return response()->json();
|
418
|
+
|
419
|
+
}
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
//emailからlogin_idに変更(オーバライド)
|
424
|
+
|
425
|
+
/**
|
426
|
+
|
427
|
+
* Get the login username to be used by the controller.
|
428
|
+
|
429
|
+
*
|
430
|
+
|
431
|
+
* @return string
|
432
|
+
|
433
|
+
*/
|
434
|
+
|
435
|
+
public function username()
|
436
|
+
|
437
|
+
{
|
438
|
+
|
439
|
+
return 'login_id';
|
440
|
+
|
441
|
+
}
|
442
|
+
|
443
|
+
}
|
444
|
+
|
445
|
+
```
|
446
|
+
|
143
447
|
|
144
448
|
|
145
449
|
仮説でnginxが原因としているため他に原因が分かる方や修正の仕方が分かる方がいましたらご教授お願いします。
|