質問編集履歴

1

認証関係のコードを追記しました。

2021/01/02 07:46

投稿

Tikka123456
Tikka123456

スコア34

test CHANGED
File without changes
test CHANGED
@@ -277,3 +277,213 @@
277
277
  }
278
278
 
279
279
  ```
280
+
281
+
282
+
283
+ 追記
284
+
285
+
286
+
287
+ LoginController.php
288
+
289
+ ```ここに言語を入力
290
+
291
+ <?php
292
+
293
+
294
+
295
+ namespace App\Http\Controllers\Auth;
296
+
297
+
298
+
299
+ use App\Http\Controllers\Controller;
300
+
301
+ use App\User;
302
+
303
+ use App\Providers\RouteServiceProvider;
304
+
305
+ use Illuminate\Foundation\Auth\AuthenticatesUsers;
306
+
307
+ use Illuminate\Http\Request;
308
+
309
+ use Laravel\Socialite\Facades\Socialite;
310
+
311
+
312
+
313
+ class LoginController extends Controller
314
+
315
+ {
316
+
317
+ /*
318
+
319
+ |--------------------------------------------------------------------------
320
+
321
+ | Login Controller
322
+
323
+ |--------------------------------------------------------------------------
324
+
325
+ |
326
+
327
+ | This controller handles authenticating users for the application and
328
+
329
+ | redirecting them to your home screen. The controller uses a trait
330
+
331
+ | to conveniently provide its functionality to your applications.
332
+
333
+ |
334
+
335
+ */
336
+
337
+
338
+
339
+ use AuthenticatesUsers;
340
+
341
+
342
+
343
+ /**
344
+
345
+ * Where to redirect users after login.
346
+
347
+ *
348
+
349
+ * @var string
350
+
351
+ */
352
+
353
+ protected $redirectTo = RouteServiceProvider::HOME;
354
+
355
+
356
+
357
+ /**
358
+
359
+ * Create a new controller instance.
360
+
361
+ *
362
+
363
+ * @return void
364
+
365
+ */
366
+
367
+ public function __construct()
368
+
369
+ {
370
+
371
+ $this->middleware('guest')->except('logout');
372
+
373
+ }
374
+
375
+
376
+
377
+ protected function authenticated(Request $request)
378
+
379
+ {
380
+
381
+ // フラッシュメッセージを表示
382
+
383
+ session()->flash('flash_message', 'ログインしました');
384
+
385
+ return redirect('/');
386
+
387
+ }
388
+
389
+
390
+
391
+ public function redirectToProvider(string $provider)
392
+
393
+ {
394
+
395
+ return Socialite::driver($provider)->redirect();
396
+
397
+ }
398
+
399
+
400
+
401
+ public function handleProviderCallback(Request $request, string $provider) // Googlelogin
402
+
403
+ {
404
+
405
+ $providerUser = Socialite::driver($provider)->stateless()->user();
406
+
407
+
408
+
409
+ $user = User::where('email', $providerUser->getEmail())->first();
410
+
411
+
412
+
413
+ if ($user) {
414
+
415
+ $this->guard()->login($user, true);
416
+
417
+ return $this->sendLoginResponse($request);
418
+
419
+ }
420
+
421
+
422
+
423
+ return redirect()->route('register.{provider}', [
424
+
425
+ 'provider' => $provider,
426
+
427
+ 'email' => $providerUser->getEmail(),
428
+
429
+ 'token' => $providerUser->token,
430
+
431
+ ]);
432
+
433
+ }
434
+
435
+ }
436
+
437
+ ```
438
+
439
+
440
+
441
+ ルーティング
442
+
443
+ ```ここに言語を入力
444
+
445
+ <?php
446
+
447
+ Auth::routes();
448
+
449
+ // グーグルログイン
450
+
451
+ Route::prefix('login')->name('login.')->group(function () {
452
+
453
+ Route::get('/{provider}', 'Auth\LoginController@redirectToProvider')->name('{provider}');
454
+
455
+ Route::get('/{provider}/callback', 'Auth\LoginController@handleProviderCallback')->name('{provider}.callback');
456
+
457
+ });
458
+
459
+
460
+
461
+ Route::get('/about','AboutContoroller@about')->name('articles.about');
462
+
463
+
464
+
465
+ Route::prefix('register')->name('register.')->group(function () {
466
+
467
+ Route::get('/{provider}', 'Auth\RegisterController@showProviderUserRegistrationForm')->name('{provider}');
468
+
469
+ Route::post('/{provider}', 'Auth\RegisterController@registerProviderUser')->name('{provider}');
470
+
471
+ });
472
+
473
+ Route::middleware('auth')->group(function () {
474
+
475
+ Route::get('/edit', 'UserController@edit')->name('edit'); // ユーザーのプロフィール編集
476
+
477
+ Route::put('/{name}/follow', 'UserController@follow')->name('follow');
478
+
479
+ Route::delete('/{name}/follow', 'UserController@unfollow')->name('unfollow');
480
+
481
+ });
482
+
483
+ });
484
+
485
+
486
+
487
+ Route::resource('/comments', 'CommentController')->only(['store']);
488
+
489
+ ```