2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【laravel6 】ログイン後にマイページにリダイレクトさせる方法

Posted at

はじめに

https://note.com/akina7/n/naa77c7020363
上記の優秀な記事の通りに実装しても上手く行かなくて詰まったので、備忘録としてログイン後にマイページにリダイレクトさせる方法をまとめておきます

前提

開発環境

  • laravel 6系

パス

web.php

Auth::routes();

Route::get('/', 'HomeController@index')->name('home');
Route::get("login/{provider}", "Auth\LoginController@redirectToProvider")->name('login.{provider}');
Route::get('login/{provider}/callback', 'Auth\LoginController@handleProviderCallback');
Route::resource('/locations', 'LocationController', ['except' => ['index']])->middleware("auth");
Route::prefix('users')->name('users.')->group(function () {
    Route::get('/{name}', 'UserController@show')->name('show');
});
login.php
protected $redirectTo = RouteServiceProvider::HOME;

【結論】ログイン後にマイページにリダイレクトさせる方法

login.php
    public function redirectTo()
    {
        if (!Auth::user()) {
            return '/';
        }
        return
            route('users.show', ['name' => Auth::user()->name]);
    }
    protected function authenticated($request, $user)
    {
        return redirect(route("users.show", ["name" => Auth::user()->name]));
    }

少し粗手ですが、、、、
ポイントは、 authenticated()を使って、認証後にやりたい処理を書くことで、リダイレクト先を変更していること!

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?