10
votes

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
Is this fresh Laravel 5.2 project? Could you show your routes.php file? - Marcin Nabiałek
Are your login and test-login routes inside the web middleware group? - patricus
@MarcinNabiałek: it is a fresh laravel 5.2 project and I think I got the problem. like @patricus mentioned, my routes were not inside the web middleware. It worked after I moved all my routes into the web middleware - Nauphal
@nauphal That's why I asked about it, please look at my answer - Marcin Nabiałek

1 Answers

21
votes

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