How to implement email verification for users in Laravel

Often, we need users to verify the email that they registered in the app. This might be because of security reasons or if we want to be sure that the communications we send to the user reach the mailbox of an existing account.

Laravel offers the easy implementation of email verification through built-in services.

Adding email verification

In the User model, add the interface MustVerifyEmail. After this, a verification email will arrive when we register a new user in our app.

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    // ...
}

Routing

Three routes need to be defined to ensure successful email verification.

  1. The route to display to the user the link they need to click in the verification email that they receive.
  2. The route to determine the flow of events when the link in the verification email is clicked on.
  3. The route to handle resending of verification emails in case the link has been lost.

We can also protect the routes so that only those users who have been verified by email have access.

This is done using the route middleware, which is implemented as follows:

Route::get('/profile', function () {
    // Only verified users may access this route...
})->middleware('verified');

Free Resources