Laravel 5.2 login session not persisting

2019-02-12 15:02发布

问题:

I have been implementing a simple authentication system on Laravel 5.2 using Sentinel.

// Route : /login
$success = Sentinel::authenticate(array(
   'email'    => $email,
   'password' => $password,
));

echo $success ? 'Login success' : 'Login failed';

So, the above code outputs Login success after the authentication code. But, the login status is not getting persisted to other requests. ie: if I check the authentication status from other requests, it is saying that I am not logged in!

// Route : test-login
echo \Sentinel::check() ? 'User is logged in' : 'User is not logged in';

I have even tried to implement a defaut laravel authencation using \Auth::attempt. But, that also giving the same thing.

Any help on this is greatly appreciated.

回答1:

In Laravel 5.2 you need to apply web group middlewere to all your routes you wan't to make sessions work. This is the major change from Laravel 5.1.

Please look at https://laravel.com/docs/5.2/routing#basic-routing

The default routes.php file looks like this at the moment:

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

EDIT

You can look also directly at https://github.com/laravel/laravel/blob/master/app/Http/Kernel.php into middlewareGroups property to know which middlewares are fired for web group middleware