Email Verification

Introduction

Many web applications require users to verify their email addresses before using the application. Rather than forcing you to re-implement this on each application, Laravel provides convenient methods for sending and verifying email verification requests.

Getting Started Fast

Want to get started fast? Install Laravel Jetstream in a fresh Laravel application. After migrating your database, navigate your browser to /register or any other URL that is assigned to your application. Jetstream will take care of scaffolding your entire authentication system, including email verification support!

Model Preparation

To get started, verify that your App\Models\User model implements the Illuminate\Contracts\Auth\MustVerifyEmail contract:

  1. <?php
  2. namespace App\Models;
  3. use Illuminate\Contracts\Auth\MustVerifyEmail;
  4. use Illuminate\Foundation\Auth\User as Authenticatable;
  5. use Illuminate\Notifications\Notifiable;
  6. class User extends Authenticatable implements MustVerifyEmail
  7. {
  8. use Notifiable;
  9. // ...
  10. }

Once this interface has been added to your model, newly registered users will automatically be sent an email containing an email verification link. As you can see by examining your EventServiceProvider, Laravel already contains a SendEmailVerificationNotification listener that is attached to the Illuminate\Auth\Events\Registered event.

Database Considerations

The Email Verification Column

Next, your user table must contain an email_verified_at column to store the date and time that the email address was verified. By default, the users table migration included with the Laravel framework already includes this column. So, all you need to do is run your database migrations:

  1. php artisan migrate

Routing

All of the routes needed to perform email verification are automatically included in Laravel Jetstream. To learn how to install Jetstream, please consult the official Jetstream documentation.

Protecting Routes

Route middleware can be used to only allow verified users to access a given route. Laravel ships with a verified middleware, which is defined at Illuminate\Auth\Middleware\EnsureEmailIsVerified. Since this middleware is already registered in your application’s HTTP kernel, all you need to do is attach the middleware to a route definition:

  1. Route::get('profile', function () {
  2. // Only verified users may enter...
  3. })->middleware('verified');

Views

All of the views needed to perform email verification are automatically included in Laravel Jetstream. To learn how to install Jetstream, please consult the official Jetstream documentation.

Events

Laravel dispatches events during the email verification process. You may attach listeners to these events in your EventServiceProvider:

  1. /**
  2. * The event listener mappings for the application.
  3. *
  4. * @var array
  5. */
  6. protected $listen = [
  7. 'Illuminate\Auth\Events\Verified' => [
  8. 'App\Listeners\LogVerifiedUser',
  9. ],
  10. ];