yadhul's avatar

how to track login and logout of user in laravel

how to track login and logout of users in laravel. can any one give brief about that.. pls and i need to list out the tracking timing of user login time and logout time...

0 likes
6 replies
pmall's avatar

You can listen to 'auth.login' and 'auth.logout' events.

3 likes
goatshark's avatar

Hey @pmall, how would one listen for these? When making my own Listeners, I have to use the Event...So if one were creating a Listener for auth.login, what would you tell the listener to 'use'? Does that question even make sense?

goatshark's avatar

@bobbybouwmann Thanks. I'll read through that right now for a sanity check. I was just heading back here to report that I figured it out.... You apparently don't need to use anything in your listener. Also, this is probably not news to anyone here, but I did see that php artisan event:generate doesn't magically make the listener when you add:

        'auth.login' => [
            'App\Listeners\AuthLoginListener',

to it. Obviously not a big deal, but I just found it interesting. Here's my listener that seems to be working --> at least I get my user object returned on the dd of $event. yay.

Thanks for the link @bobbybouwmann

<?php

namespace App\Listeners;

use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class AuthLoginListener implements ShouldQueue
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  SlotWasModified  $event
     * @return void
     */
    public function handle($event)
    {
        dd($event);
    }
}
1 like
Szendi_Attila's avatar

If I want to broadcast these built in events, how can I do that? Now I just overwrite the AuthenticatesUsers Trait methods (authenticated, loggedOut) in the LoginController to dispatch my own events. Is there a better way for this?

Please or to participate in this conversation.