\Auth::user() is null in 5.3.6?

亡梦爱人 提交于 2019-12-12 04:58:47

问题


This is about Laravel 5.3.6

I am able to login successfully and I can check Auth User after login. I can show the exact location where Auth::guard() has current user object. Below are the details.

  1. Go to Login Controller
  2. Go to AuthenticatesUsers Trait
  3. Go to sendLoginResponse method. User reaches here successfully because user is authenticated successfully.

here I can check $this->guard()->user() has current user value. But when control reaches to Role controller....I tried to access it like this dd(Auth::guard()); and value was null. I also added reference below in Role Controller.

use Illuminate\Support\Facades\Auth;

Below is my route for Role Controller.

Route::group(['middleware' => ['auth']], function () {
    Route::get('/Roles',              
        array (
            'uses'  => 'Website\Role\RoleController@index',      
            'as'    => 'Roles'
        )
    );
});

Did you face this kind of issue in Laravel 5.3.6?

Output of \Auth::guard() is below.

SessionGuard {#162 ▼
  #name: "web"
  #lastAttempted: null
  #viaRemember: false
  #session: Store {#165 ▶}
  #cookie: CookieJar {#167 ▶}
  #request: Request {#40 ▶}
  #events: Dispatcher {#5 ▶}
  #loggedOut: false
  #tokenRetrievalAttempted: false
  #user: null
  #provider: EloquentUserProvider {#158 ▶}
}

回答1:


Kernel.php file was like below

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
];

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];

I changed it to like below.

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \App\Http\Middleware\EncryptCookies::class,
    \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
    \App\Http\Middleware\VerifyCsrfToken::class,
];

protected $middlewareGroups = [
    'web' => [
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],

    'api' => [
        'throttle:60,1',
        'bindings',
    ],
];


来源:https://stackoverflow.com/questions/39364297/authuser-is-null-in-5-3-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!