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

markokaup's avatar

Set session value after login - Login not working

I have login listener which should set one value to session after login.

EventServiceProvider.php (classes is imported)

    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ],
        Login::class => [
            AddToSessionAfterLogin::class,
        ],        
    ];

AddToSessionAfterLogin.php

    public function handle(Login $event)
    {
        session(['user_id' => $event->user->id]);
    }

Somehow login doesn't work any more. When I try to login, nothing happens and when I try again, I get.

Whoops! Something went wrong.
These credentials do not match our records.

I have flushed all caches and event dump autoload. Cleared browser cache and cookies. Still same. Any idea what's wrong here?

Thanks!

0 likes
4 replies
Nakov's avatar

Any error message in your storage/logs file ? With more context maybe?

You can try adding Log::info in your handle method and see if that code is ever executed.

markokaup's avatar

@Nakov Logs are empty and I dd' in code, it is executed. Session value is also stored but login just won't work.

Nakov's avatar
Nakov
Best Answer
Level 73

@markokaup so if you comment out the event, does it work? I doubt that this is what broke your login.

You can try commenting out the event, try also changing the key from user_id to something else.

markokaup's avatar

@Nakov Okey, we have solved this problem! I changed the key user_id and login works! I actually had to change it anything else than user_whatever bc that user_ key value somehow messed up login session..

I should have seen this but sometimes code makes you blind!

Thank you.

Please or to participate in this conversation.