10

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.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Nauphal
  • 6,194
  • 4
  • 27
  • 43

1 Answers1

21

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

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • 3
    Laravel is getting sucks. These new versions are more complex over a over again. – manix Jan 29 '16 at 21:33
  • 1
    @manix I disagree. What do you expect from new version of software? Probably new function and when new functions are added you always need to add complexity to application. However I can agree, that documentation is missing detailed description so sometimes it's hard to figure out how it is working. And always you can use the older version on Laravel if the functionality is fine for you – Marcin Nabiałek Jan 29 '16 at 22:10
  • Unfortunately, it doesn't work for me (even with fresh installed Laravel 5.2.29). Btw, currently, as to docs, "The default `routes.php` file is loaded by the `RouteServiceProvider` and is automatically included in the `web` middleware group" – zhekaus Apr 04 '16 at 17:00
  • Yes. In 5.2 the middleware web group is added by default to routes in the App\Http\Controllers namespace. Look at the mapWebRoutes function in the Providers/RouteServiceProvider.php file. – JimB Nov 22 '16 at 21:53
  • in laravel 5.5 this is done by default in RouteServiceProvider for all routes in routes/web.php. Defining it twice might cause problems https://github.com/laravel/framework/issues/13000 – StackExploded Feb 13 '18 at 16:06