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

RavenFaramond's avatar

Login event doesn't fire at first login

So, I want to update a user's last login time whenever they log in and I've run into this problem. I'm using Laravel's own login (which is based on the AuthenticatesUsers trait) and its own Login event (Illuminate\Auth\Events\Login) in a subscriber. They're bound like this in the subscribe() method of the listener:

    $events->listen(
        'Illuminate\Auth\Events\Login',
        'App\Listeners\AuthEventListener@onLogin'
    );

Now, I know the onLogin method is always hit (whenever I dd or do logging in it, it's triggered) but it doesn't update the user's last login column in the database. However, when I log out and login again, this time the column is updated. It's like that for every other user. Doesn't update the database at first login, but updates it with no problem if you log in for a second time after logging out.

Anybody has any idea why that might be happening?

Thanks in advance.

0 likes
11 replies
jlrdw's avatar

Sure you didn't use remember login, and user is still logged in.

RavenFaramond's avatar

No, that's not it. I've just realized that even though the method is hit on login, the field is updated only when the user manually logs out. Shouldn't it just get updated the moment the user logs in?

jlrdw's avatar

only when the user manually logs out. Shouldn't it just get updated the moment the user logs in?

How else are they logging in if not manually?

tykus's avatar

@jldrw the log out was manual as opposed to the login. Contribute positively or not at all.

@RavenFaramond can you share the Event Listener code?

1 like
jlrdw's avatar

@tykus

Contribute positively or not at all.

Listen you, don't start, I freaking mis-read.

Have a nice day.

RavenFaramond's avatar

@tykus

<?php

namespace App\Listeners;

use Carbon\Carbon;

class AuthEventListener
{
    /**
     * Handle successful login event.
     * 
     * @param $event
     */
    public function onLogin($event)
    {
        \Log::info('method hit');
        
        app('App\Contracts\UserRepositoryInterface')
                    ->edit($event->user, ['last_login_at' => Carbon::now()]);
    }


    /**
     * Register the listeners for the subscriber.
     *
     * @param  \Illuminate\Events\Dispatcher  $events
     */
    public function subscribe($events)
    {

        $events->listen(
            'Illuminate\Auth\Events\Login',
            'App\Listeners\AuthEventListener@onLogin'
        );

    }
}

When I log in, I see 'method hit' in Debugbar's logs but the last_login_at field doesn't get updated. When I log out, it's only then updated.

tykus's avatar

Weird. Can you check if the $event->user is available to the onLogin() method every time via Log::info()? Is the concrete implementation of UserRepositoryInterface bound in time?

1 like
RavenFaramond's avatar

Okay, all of a sudden it hit me: I forgot to add the last_login_at in the $fillable array. Sorry for the inconvenience. It's now working as expected. I'm kinda mad at myself.

Still, though, the field gets updated on logout even though no events are fired. Neither do I see any queries updating the last_login_at field in Debugbar's stacks. That part still baffles me.

tykus's avatar

Sometimes we concentrate on the complex and forget about the simple... Good job getting it sorted nonetheless

1 like
RavenFaramond's avatar

All right, in case this might be helpful for anyone:

As for the second issue, I had used timestamp() in migrations for the last_login_at field whereas I should've used dateTime(). MySQL updates timestamps automatically when there's a change.

Thanks to those who tried to help.

Please or to participate in this conversation.