Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5.2 login session not persisting

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.

like image 493
Nauphal Avatar asked Dec 30 '15 11:12

Nauphal


1 Answers

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

like image 94
Marcin Nabiałek Avatar answered Oct 10 '22 23:10

Marcin Nabiałek