Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Romentigo's avatar

Session not persisting after login, only with remember.

I've found out that session is destroyed after I login, but it keeps alive if I check "Remember me". I'm using default Auth. My routes:

Route::group(['middleware' =>['web']], function ()
{
    Route::get('/', 'RenderController@welcome')->name('welcome');
    Route::get('/login', function () {
        return view('login');
    });

    Auth::routes();

    Route::group(['middleware' => ['web','auth']], function () {
        Route::get('/profile', [
            'uses' => 'Api\ProfileController@index',
            'as' => 'user'
        ]);
        Route::get('/instruments', [
            'uses' => 'Api\InstrumentController@index',
            'as' => 'instruments'
        ]);
        Route::get('/logout', 'Auth\LoginController@logout');
    });
});
0 likes
7 replies
bobbybouwmann's avatar

It seems that you're overusing the web middleware group here. Not sure what happens if you apply the same middleware twice.

For your second group, you don't need to specify the web middleware group again. Also, by default Laravel already adds the web middleware group to your routes unless you made some changes in your RouteServiceProvider.

Romentigo's avatar

@bobbybouwmann Hmm, yep, it worked just fine, thanks. One more question. It can sound weird, by I'm trying to get Auth info by Api request and it says that can't get property of non an object. For Api routes I use web middleware. How can I get Auth data? Also, session is destroyed after I try to get data from Api.

bobbybouwmann's avatar

@romentigo Just set the time of the session very high. By default, this is set to 120 minutes which translates to 2 hours

// config/session.php

'lifetime' => env('SESSION_LIFETIME', 120),

Set it to 5259492 which is 10 years in minutes.

Snapey's avatar
Snapey
Best Answer
Level 122

api routes should be stateless. If you want api routes to use the user session, move them into the web.php file.

Romentigo's avatar

No, the problem wasn't in session lifetime. Problem was in destroying session every time when I tried to use API routes.

Romentigo's avatar

Thanks a lot! I moved all my API routes to web.php and now it works fine. Session isn't destroyed anymore. Thanks to all for help

Please or to participate in this conversation.