DEV Community

Arman Rahman
Arman Rahman

Posted on

How To Enable Email Verification On Laravel

Email verification is the process of checking and authenticating emails that users have been provided in the registration form.

To enable this feature we need to implement MustVerifyEmailinterface in our User model.

use Illuminate\Contracts\Auth\MustVerifyEmail;
…

class User extends Authenticatable implements MustVerifyEmail
{
…
}
Enter fullscreen mode Exit fullscreen mode

After that, an email will be sent out when a user registers with a link to verify their email.

However, we still need to add a middleware to our routes where we want to restrict access to unverified users.

We will create a new route called ‘only-verified’ and we will add ‘auth’ and ‘verified’ middleware. The auth middleware prevents access to guests and the verified middleware checks whether the user has verified their email.

Here is an example:

Route::get('/only-verified', function () {
   return view('only-verified');
})->middleware(['auth', 'verified']);

Enter fullscreen mode Exit fullscreen mode

Top comments (0)