Why laravel 6 auth returns false after redirecting by using custom guard?

前端 未结 4 991
醉梦人生
醉梦人生 2021-01-18 23:31

I am trying to make auth through laravel package using admins table. In the project directory I added admin guard into config/auth.php

\'providers\' => [
         


        
4条回答
  •  悲哀的现实
    2021-01-19 00:16

    Please note that the Auth::check doesn't work on construct. this is because the middleware hasn't run yet, so Auth::check() should return false or null when you try to check in construct.

    In your login controller, why are you using two redirectto?

       protected $redirectTo = '/admin/dashboard';
       protected function redirectTo()
       {
         return '/admin/dashboard';
       }
    

    it is better to stick with one :-)

    inside your Admin.php , add this:

    protected $guard = 'admin';
    

    for your web.php routes, replace

    Route::group(['namespace' => $namespace,'prefix' => 'admin',  'middleware' => ['auth']  ], function () {
    Route::get('dashboard', 'DashboardController@index')->name('dashboard');
    });
    

    with

    Route::group(['namespace' => $namespace,'prefix' => 'admin',  'middleware' => ['auth:admin']  ], function () {
    Route::get('dashboard', 'DashboardController@index')->name('dashboard');
    });
    

    finally, in DashboardController.php

    replace the

            /* dd(Auth::check()); */ //return false : just want to show you
    

    With:

        $this->middleware(function ($request, $next) {
            dd(Auth::check());  //return false : just want to show you
            die;    
        });
    

    Auth::check() should return true!

提交回复
热议问题