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

twoarmtom's avatar

Event Listener Not Working

I'm using Laravel 11, and have the following Listener created to monitor for Lockout Events. The Lockout event is NOT logged, nor is the notification is not sent to the Super Admin. Any suggestions as to why this isn't working? I have this similar functionality that is working in a Laravel 10 application. The only difference is the Laravel 10 application is using Breeze, and the Laravel 11 application is using Jetstream.

Here is my Listener

<?php

namespace App\Listeners;

use App\Models\User;
use App\Notifications\LockedOutUser;
use Illuminate\Auth\Events\Lockout;
use Illuminate\Support\Facades\Log;

class UserLockedOutNotification
{
    /**
     * Handle the event.
     */
    public function handle(Lockout $event): void
    {
        // Log the lockout event
        Log::info('User locked out due to too many login attempts.', ['request' => $event->request->input('email')]);
        $user = User::role('Super Admin')->first();
        $user->notify(new LockedOutUser($event->request->input('email')));
    }
}

And this is the Notification, I'm hoping to use the Slack Channel, but for troubleshooting purposes I'm using the Mail option and this is working with all other notifications.

Any help or suggestions would be greatly appreciated!

UPDATE: If I use the FortifyServiceProvider to log the event and send the notification it works, but it doesn't log through my Listener.

RateLimiter::for('login', function (Request $request) {
            $throttleKey = Str::transliterate(Str::lower($request->input(Fortify::username())).'|'.$request->ip());
            return Limit::perMinute(5)->by($throttleKey)->response(function ($request, $params) {
                Log::info('User locked out due to too many login attempts.', ['request' => $request->input('email')]);
                $user = User::role('Super Admin')->first();
                $user->notify(new LockedOutUser($request->input('email')));
                return back()->withErrors([
                    'message' => "Too many login attempts. Please try again in {$params['Retry-After']} seconds"
                ]);
            });
        });
0 likes
3 replies
LaryAI's avatar
Level 58

To troubleshoot why your event listener for the Lockout event isn't working in your Laravel 11 application, let's go through a few potential issues and solutions:

  1. Event and Listener Registration: Ensure that your event and listener are properly registered in the EventServiceProvider. In Laravel, you need to map your events to listeners. Check your app/Providers/EventServiceProvider.php file and make sure it includes:

    use Illuminate\Auth\Events\Lockout;
    use App\Listeners\UserLockedOutNotification;
    
    protected $listen = [
        Lockout::class => [
            UserLockedOutNotification::class,
        ],
    ];
    
  2. Caching Issues: If you have recently added or modified your event listener, you might need to clear the cached events. Run the following command to clear the cache:

    php artisan event:clear
    php artisan cache:clear
    
  3. Check the Listener Logic: Ensure that the logic inside your listener is correct. Specifically, verify that the User::role('Super Admin')->first() is returning a valid user. If it returns null, the notify method will not be called. You might want to add a check or log the result to ensure a user is found:

    public function handle(Lockout $event): void
    {
        Log::info('User locked out due to too many login attempts.', ['request' => $event->request]);
    
        $user = User::role('Super Admin')->first();
        if ($user) {
            $user->notify(new LockedOutUser($event->request->input('email')));
        } else {
            Log::warning('No Super Admin user found to notify.');
        }
    }
    
  4. Notification Configuration: Since you mentioned that other notifications are working, ensure that your mail configuration is correct in your .env file. Double-check that the mail driver and credentials are properly set.

  5. Testing the Event: You can manually trigger the Lockout event to test if your listener is working correctly. Use Tinker or a route to dispatch the event:

    event(new \Illuminate\Auth\Events\Lockout(request()));
    

    This can help you determine if the issue is with the event not being fired or with the listener not handling it correctly.

  6. Check for Errors: Look at your Laravel logs (storage/logs/laravel.log) for any errors or warnings that might give you more insight into what's going wrong.

By following these steps, you should be able to identify and resolve the issue with your Lockout event listener. If the problem persists, consider checking for any differences in configuration or package versions between your Laravel 10 and Laravel 11 applications that might affect event handling.

Talinon's avatar

@twoarmtom Since you're using queues, my suspicion is that you haven't restarted your queue workers for the changes in the application code to take effect. The queue worker won't pick up the changes in your code because it resides in memory. Restarting the workers will force the queue worker to use the new changes.

Snapey's avatar

Try point 5 from Lary's answer first. If that works then you need to check why fortify is not generating this event or if it is sending a different event.

Please or to participate in this conversation.