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

Top comments (3)

Collapse
 
stsryanholton profile image
Ryan Holton

Hi, I'm trying to achieve a queued email verification email for my project. My Laravel project is actually purely just an API backend to a NuxtJS frontend, and as such my fortify.home config is:

'home' => env('SPA_URL', 'https://example.com/account/domains/')
Enter fullscreen mode Exit fullscreen mode

So when I click the email verification button I need it to redirect to this, but after extending the SendEmailVerificationNotification listener the home url isn't retained and ends up just redirecting to localhost which is my API's page which doesn't exist. How can I retain everything about the verification email?

Collapse
 
ahmadtibi profile image
ahmadtibi

Extending the SendEmailVerificationNotification shouldn't affect where you redirect after clicking the email verification button. have you maybe tried adding a redirect at the end of the sendEmailVerificationNotification() method?

Collapse
 
duffn profile image
Nicholas Duffy

Thank you, this was very helpful.