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

前端 未结 4 995
醉梦人生
醉梦人生 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-18 23:56

    The problem is in your routes file:

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

    You are using the default guard with auth middleware. After you are logged in with admin guard you may not be logged in by your default web guard. That is why it fails and tries to redirect you to login page:

    When I try to login, after submitting valid details it does not redirecting me to dashboard, nothing happening. Also when I try for open forcefully /dashboard it take me to login page.

    Instead, you should specify in your group that you are using the admin guard:

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

    However, you already specified in your DashboardController to use $this->middleware('auth:admin');, so there is no need to specifiy it in the route group again. The following is enough and reduces the likelihood to create an error:

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

提交回复
热议问题