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

tomirons's avatar

Logout Event

In 5.2 I'm trying to clear the sessions when a user logs out. If they don't and they login with a different account, it will show a character that they selected from a different account.

The following worked in 5.0 but it's not working now...

$events->listen('auth.logout', function () {
    session()->flush();
});
0 likes
9 replies
stefanwegner's avatar

So, maybe you should check on the "new login" if you have a logged in user and log him out manually - and flush the session in this particular case ?

Dan's avatar

Is the event now called 'Illuminate\Auth\Events\Logout' ?

tomirons's avatar

I shouldn't have to, it should work... maybe the listener is wrong? I tried to dump some variable on the auth.logout and it didn't trigger.

ienderli's avatar

the last event class is correct, there is a list of some changed event names in the docs, https://laravel.com/docs/5.2/upgrade , do a ctr+f and for events and there is table. I like the class name set up better then the dot notation, mainly because of how it looks...

tomirons's avatar

So how would I trigger an event on logout now?

ienderli's avatar

If you are using an auth driver registered with laravel, the event is fired for you, then you just have to listen for it. if it is not being fired for you then you could create you on event and then fire it inside the controller method that handles your logout route.

if you are just listening for that event auth event I would write a listener, App\Listeners\UserLoggedOut, then in App\Providers\EventServiceProvider you should see an array, listen. Register your listener in that array, if it were named the same as the example I gave, you could register the listener like below. A listener has to have a handle method which will be called when the even is fired. if you want to listen for all User Auth events you can create an event subscriber class that has methods like onLogout, onLogin, etc.

Reading through the examples in the docs really helped me understand the event system.

     */
    protected $listen = [
        'Illuminate\Auth\Events\Logout' => [
            'App\Listeners\UserLoggedOut',
        ],
    ];
3 likes

Please or to participate in this conversation.