Ad

Laravel 5 Authentication Middleware Always Redirects To Root Or Login

When I protect routes in Laravel 5 it works well when I'm not logged in because it redirects the protected routes to the login page but once I login and try to access the protected routes it redirects me to the root route. For example if I try to access /people or /people/1 it will redirect me to /

Here's my routes.php file:

Route::get('/', function () {
 return view('welcome');
});

Route::group(['middleware' => ['auth']], function () {
 Route::resource('people', 'PeopleController');
 Route::resource('people.checkins', 'CheckinsController');
 Route::model('checkins', 'Checkin');
 Route::model('people', 'Person');

 Route::bind('checkins', function($value, $route) {
    return App\Checkin::whereId($value)->first();
 });
 Route::bind('people', function($value, $route) {
    return App\Person::whereId($value)->first();
 });
});

Route::group(['middleware' => 'web'], function () {
 Route::auth();

 Route::get('/home', '[email protected]');
});
Ad

Answer

If you are going to be using Auth you should have the 'web' group applied to those routes as well.

You can adjust your route group that is using the 'auth' middleware to:

Route::group(['middleware' => ['web', 'auth']], function () {
    // ...
});

UPDATE For Laravel 5.2.27. If you have installed a fresh copy of laravel/laravel >= 5.2.27 Your routes will be wrapped in a group that applies the 'web' middleware by default now. This is only for fresh installs as this change is to App\Providers\RouteServiceProvider which an upgrade to laravel/framework will not touch.

Ad
source: stackoverflow.com
Ad