Auth () & # 8594; user () is null in Laravel 5.2

I am just updating the composer to Laravel 5.2 and cannot view password protected pages. Basically the below line of code does not work.

auth()->user() 

Can anyone guess why this is not working?

+4
source share
4 answers

Verify that routes that require sessions (that are used by Auth) are behind the web middleware group .

Route::group(['middleware' => 'web'], function () {
    // your routes
});

, 5.2. . , cookie csrf.

+13

Laravel 5.2 , Auth, .

/Http/Kernel.php web middlewares.

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
];
+4

- . , guard. , , . . \Auth::guard($guard)->user()

0

For those who don’t want to blindly add middleware to routes, you just need to add classes that manage cookies and sessions to the corresponding middleware group ( apiin my case). For me, these classes are where:

\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Session\Middleware\StartSession::class,

Here's how my variable ended App\Http\Kernel::$middleWare:

protected $middlewareGroups = [
    'web' => [
        ...
    ],

    'api' => [
        'throttle:60,1',
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Session\Middleware\StartSession::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
        \Illuminate\Auth\Middleware\Authenticate::class
    ],
];

Using Laravel 5.3

0
source

Source: https://habr.com/ru/post/1622040/


All Articles