Skip to content
Advertisement

Laravel: How to load session variables for ‘remembered’ user session (LoginController is Bypassed)

I have built an application in Laravel 7.0 and when a user logs in, I drop some application wide variables in to session variables using by adding this to the LoginController:

protected function authenticated ($user)
{
    Session::put('notifications', auth()->user()->unreadNotifications);
}

If have verified the issue by logging out when this occurs and logging back in solves the problem (because the session variables are created as expected).

Unfortunately it seems that when a user returns to the site and it ‘remembers’ their previous session, the LoginController is bypassed and the session variables aren’t loaded. Does anyone know if this type of login fires an event I can regenerate the session variables in? Or a more elegant way to handle this?

Thanks!

Advertisement

Answer

There is a Login event, IlluminateAuthEventsLogin, that is fired and you can check the guard method viaRemember to see if they were authenticated via the remember me cookie.

You can listen for this Login event instead of using that authenticated method and you will cover both cases.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement