Auth not persisted in Laravel 5.2

I have my auth doing this on login.

if (Auth::attempt($userdata)) {
    dd(Auth::user()); //this shows the user just fine, 
                      //which proves that the auth driver is working.
    return redirect()->intended('dashboard');
} 

However, after being redirected to the dashboard. Auth doesn't seem to persist. If I do, dd(Auth::user())or even just Auth::check(), it returns null.

Here's the route:

Route::group(['middleware' => ['web']], function () {
     Route::get('test',function(){
       dd(Auth::user()); //returns null
        echo Auth::user()->name; // returns Trying to get property of non-object
     });
});

What am I doing wrong?

The strange thing is that she worked last night. It just magically stopped working.

+1
source share
2 answers

"auth" "" ​​ . . , , "auth" .

"auth" , , Auth:: user() .

, , , Laravel Auth , - .

Auth Laravel.

0

, laravel.
. , Laravel 5.2
, , , .

.

Route::post('app/login', 'Auth\AuthController@doLogin');

Route::group(['middleware' => ['web','auth']], function () {
     Route::get('test',function(){
        dd(Auth::user());// was always returning null
     });
});

,

Route::group(['middleware' =>[ 'web']], function () {
   Route::post('app/login', 'Auth\AuthController@doLogin');
});

Route::group(['middleware' => ['web','auth']], function () {
     Route::get('test',function(){
        echo Auth::user()->name;
     });
});

, , , "web".

0

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


All Articles