DEV Community

Cover image for Fire an action when user is logged in or verified with Laravel
Ariel Mejia
Ariel Mejia

Posted on

Fire an action when user is logged in or verified with Laravel

There are case when we need to fired an event, a job or maybe notify something when user is logged in or the account is verfied, there are two ways of handle this actions.

Fired an event when user is logged in:


On LoginController

Lets suppose we want to send a notification when the user is logged in, in this case the "LoginController" does provide a "redirectTo" method this is the only method available when the user is already validated and logged in, so here we need to fired an event or any other logic that you would need:

app/Http/Controllers/Auth/LoginController.php
Enter fullscreen mode Exit fullscreen mode
    public function redirectTo()
    {
        auth()->user()->notify(new StatusNotification());
    }
Enter fullscreen mode Exit fullscreen mode

There is another way to handle this event:

Using the Login event:

app/Providers/EventServiceProvider:
Enter fullscreen mode Exit fullscreen mode
    protected $listen = [
        Login::class => [
            SendEmailStatusNotification::class,
        ],
    ];
Enter fullscreen mode Exit fullscreen mode

In this example you need to create your listener "SendEmailStatusNotification" and there on "handle" method you can get the auth user and notify.

In you listeners you can inject the Login event to get the event user for example, this is another way of fire the notification:

public function handle(Login $event)
{
   $event->user->notify(new StatusNotification);
   // or use the typicall auth facade
   auth()->user()->notify(new StatusNotification);
}
Enter fullscreen mode Exit fullscreen mode

Fired an event on verifiedAccount:


You can use the "verified" method on VerificationController:

app/Http/Controllers/Auth/VerificationController.php
Enter fullscreen mode Exit fullscreen mode
    public function verified(Request $request)
    {
        $request->user()->notify(new StatusNotification());
    }
Enter fullscreen mode Exit fullscreen mode

Using the Verified event:

The same way you can handle this approach as an event in EventServiceProvider:

   use use Illuminate\Auth\Events\Verified;
   ....

    protected $listen = [
        Verified::class => [
            SendEmailStatusNotification::class,
        ],
    ];
Enter fullscreen mode Exit fullscreen mode

Lastly, remember if you need to fired an event when the user is getting register,

Fired an event when user is registered


Literally there is a method named "registered":

app/Http/Controllers/Auth/RegisterController.php
Enter fullscreen mode Exit fullscreen mode
    public function registered(Request $request, $user)
    {
        $user->notify(new NewsletterNotification());
    }
Enter fullscreen mode Exit fullscreen mode

Using the Registered event:

You can use the same Event approach:

   use Illuminate\Auth\Events\Registered;
   ....

    protected $listen = [
        Registered::class => [
            SendWelcomeEmailNotification::class,
        ],
    ];
Enter fullscreen mode Exit fullscreen mode

Personally I feel more elegant way to handle the events by using the events on "EventService" provider and it looks more like a good standard, but here are both ways to handle this

There are more events that work like this like: "Logout" event and "logout" method on "LoginController" but this examples show the most common cases.

Thanks for reading.

Top comments (0)