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

appyapp's avatar

Updating session after login with extra values.

In my Auth/LoginController.php

I have added

protected function authenticated(Request $request, $user)
{
    session(['hello' => 'world']);
}

It works well if a user actually submit their username/password using the login form.

However, I have noticed this function is never called if a user is logged in via "Remember me". So the session is never updated with that value above.

What method do I need to override for both cases?

0 likes
4 replies
rwdevguy's avatar
rwdevguy
Best Answer
Level 11

For this you could hook in to the LoginEvent.

Run:

php artisan make:listener AddToSessionAfterLogin

Then n your App\Providers\EventServiceProvider add the following to the $listen array:

\Illuminate\Auth\Events\Login::class => [
    \App\Listeners\AddToSessionAfterLogin::class,
],

Obviously, feel free to import these classes if you want.

Then in your AddToSessionAfterLogin class, which you've just created, you can add the necessary logic to the handle() method i.e.:

public function handle(Login $event)
{
    session(['hello' => 'world']);
}

FYI, you can access the authenticated user with $event->user.

appyapp's avatar

Hmm - I was thinking If I could keep it in my LoginController ...

Jaytee's avatar

There are a couple of options:

  1. Flush the sessions, so that every user will need to re-authenticate. This will then trigger the authenticated method so you can set session data. If the session data changes periodically or based on conditions, then this may not be the best approach.

  2. You could possibly add a global middleware which is ran on every request. This middleware will check if a user is authenticated, and then handle the session. If they are not authenticated, then don't set the data (unless you want to do that)

Hooking into the login event probably won't work since that is only going to be fired when a user logs in I believe. I don't think this is fired on every request (although I may be wrong).

appyapp's avatar

Ok I have tested it and the Login event is fired in both cases.

Please or to participate in this conversation.