Laravel 5.8 New Email Verification

In this tutorial you will learn about the Laravel 5.8 New Email Verification and its application with practical example.

Laravel 5.8 Email Verification

Laravel 5.8 provides built-in email verification system for newly registered user. Using this, newly registered user will get an email with an account activation link. This activation link is used for account verification. When activation link is clicked, this will make user account verified and active for the application. Once user account is activated then it will be allowed to access protected routes; which can be accessible only by verified accounts.

In this step by step tutorial, we’ll show you how to setup email verification system for newly registered user in laravel 5.8.

Install Laravel 5.8

First of all we need to create a fresh laravel project, download and install Laravel 5.8 using the below command

Make sure you have composer installed. Now, lets switch to the project directory and start the development server using following artisan command –

Now, open the following URL in browser to see the output –

http://localhost:8000/

Output:-

laravel-5-7-crud-tutorial-with-example-2

Configure Database In .env file

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Now, use the below command to create default laravel tables automatically.

Email Configuration

Since, we will be sending an email with an account activation link. To send email, we need to add email configuration or smtp details in .env file.

Authentication Scaffolding

Now, use the below command to generate authentication scaffolding –

This command will generate required Controller files, views and add routes in our web.php routes file that are required for the authentication. If you open your route file (resources/routes/web.php) you will have a resource route added in it like below –

Email Verification Setup

To implement email verification we need to implement the MustVerifyEmail Contracts in our User model. Lets open the App/User.php file and add the “Implements MustVerifyEmail” to the class declaration as given below –

Add Route

Last thing we need to do is add the verify middleware to the auth routes.

The “verified” middleware is used to protect routes from unverified users.

We can add the “verified” middleware to the route itself as given below –

Example:-

or we can also use it in controllers as following –

Example:-

Add Middleware In Controller

In this example we will be adding Middleware in controller’s constructor to protect access. Lets open app/controllers/HomeController.php and put $this->middleware([‘auth’, ‘verified’]); inside of constructor.

app/controllers/HomeController.php

This will block unverified users to access laravel 5.8 dashboard, and only allow when users have verified their email. Now when you register a new user, it will send a verification email to that user and redirect to verify view (same will happen when a old user tries to access home route).

Start Development Server

Start the development server using following artisan command –

Test Laravel 5.8 Email Verification

At this stage, we are ready to test the user registration and login system with email verification system.

Now, you should able to see your registration page at

http://localhost:8000/register

laravel-5-8-email-verification-example-2

When you fill the form and submit it; you will get an alert for email verification in your account as follows –

laravel-5-8-email-verification-example-3-1

You will also get an account verification link in your email account as follows –

laravel-5-8-email-verification-example-4

Go ahead and verify your account, on successful verification you will be allowed to access protected application routes.

laravel-5-8-email-verification-example-5

In this tutorial we have learn about the Laravel 5.8 New Email Verification and its application with practical example. I hope you will like this tutorial.