DEV Community

ahmadtibi
ahmadtibi

Posted on

Queueing up Laravel Fortify email verification for better performance

After a long break from laravel (last time I used it was around version 5.7), I recently got back into because of more free time.

I noticed there was a relatively new package called Fortify which allows you to scaffold the authentication process, it seemed pretty useful; however after diving deep into it, I realized that after registering a new user and sending an email verification there would be blocking request for a few seconds so it was obvious to me that the email verification was in fact not being queued.

Queuing up the email verification

I'm gonna start with a clean laravel 8 project and add fortify to it

Installing fortify

composer require laravel/fortify

Overriding the email verification notification and making it a job that we can queue

If we go into the user model there's a method called sendEmailVerificationNotification() which we can actually override to modify how fortify sends the email verification notification.

In order to override we first have to make a new notification that will implement the ShouldQueue Interface, and extend the VerifyEmail notification that is provided by Fortify.

Creating the notification and modifying it to our needs

To create a new notification we can just use laravel's built in php artisan helper.

php artisan make:notification VerifyEmailQueued

After creating the notification we should make it extend VerifyEmail and implement the ShouldQueue.

The notification class should look like this:
VerifyEmailQueued.php

<?php

namespace App\Notifications;

use Illuminate\Auth\Notifications\VerifyEmail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;

class VerifyEmailQueued extends VerifyEmail implements ShouldQueue
{
    use Queueable;

    /**
     * Create a new notification instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }


    /**
     * Get the array representation of the notification.
     *
     * @param  mixed  $notifiable
     * @return array
     */
    public function toArray($notifiable)
    {
        return [
            //
        ];
    }
}

Enter fullscreen mode Exit fullscreen mode

Overriding the sendEmailVerificationNotification() in the User model

Now there's one more thing left to do and it's overriding the sendEmailVerificationNotification() method in the User model

Go to User.php and add the following method:

public function sendEmailVerificationNotification()
    {
        $this->notify(new VerifyEmailQueued);
    }
Enter fullscreen mode Exit fullscreen mode

Now there's one more thing left to do and it's running the artisan command to listen for jobs and queue them up.

php artisan queue:work

If everything is working correctly you should see that the VerifyEmailQueued notification should be processed and an email should be sent to the User. It will look something like this in the terminal:
Terminal image

Discussion (0)