Ad

Auth Session Killed After Redirect | Laravel 5.2

- 1 answer

I made a simple login form. I log my user with :

Auth::loginUsingId($user->id, true);

But when I redirect the user to the ClientController I'm redirect to the login form, the Auth session is not persitent.

return redirect()->action('[email protected]');

My routes :

Route::group(['middleware' => 'web'], function() {
    Route::get('/', '[email protected]');
    Route::post('/', '[email protected]');
});
Route::group(['prefix' => 'admin', 'middleware' => 'web'], function() {
    Route::get('/', '[email protected]');
});

Route::group(['prefix' => 'client', 'middleware' => ['auth', 'web']], function() {
    Route::get('/', '[email protected]');
});
Ad

Answer

The web middleware needs to kick in before the auth middleware because the web middleware is responsible for booting up your session. Switch the order around like this:

Route::group(['prefix' => 'client', 'middleware' => ['web', 'auth']], function() {
    Route::get('/', '[email protected]');
});

However, while we're on this subject, you can nest route groups within another route group so to prevent an error like this in the future, I would just recommend nesting everything inside the web middleware like this:

Route::group(['middleware' => 'web'], function() {
    Route::get('/', '[email protected]');
    Route::post('/', '[email protected]');

    Route::group(['prefix' => 'admin'], function() {
        Route::get('/', '[email protected]');
    });

    Route::group(['prefix' => 'client', 'middleware' => 'auth'], function() {
        Route::get('/', '[email protected]');
    });
});
Ad
source: stackoverflow.com
Ad