Force User to Verify Email after Registration in Laravel

In this post, I will share with you how to implement email verification for your Laravel 8, and 9 projects. New user must be verify email address after registration in your application. We will send activation code on register email address to verify email in your Laravel app. Since we have authentication already we need to verify the given email is active.

In laravel old version we are doing email verification process manually, but in Laravel 7 and above versions, they provide in build email verification setup for new registered users to must have to verify his email before proceed. You just need to make some basic setup with need to use middleware, routes and mail configuration. After registration sending an email to registered user is very important if we are developing a secure application.

Please like this video and subscribe to my YouTube channel.

There are few simple steps which is very easy to follow and to implement in laravel 8 application. We will see this tutorial from scratch. Email verification is laravel is very simple process to create.

Just follow this tutorial from scratch and you will set up for email verification in laravel 8 project.

Step 1: Laravel Installation


First of all, we need to get fresh Laravel 8 version application using bellow command because we are going from scratch, So open your terminal OR command prompt and run bellow command:

composer create-project --prefer-dist laravel/laravel yourproject

composer require laravel/ui

Step 2: Create Database & Connect Database Configuration


In this step, to connect database with application, Open .env file from application root. Search for DB_ and update your details. So let’s add username, password etc.

DB_CONNECTION=mysql
DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=username
DB_PASSWORD=password

Step 3: Email Configuration


Here, we need to add email configuration, Open .env file. We are sending email after user registration so we need to add email smtp details for send email.

MAIL_DRIVER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME=example@gmail.com
MAIL_PASSWORD=password
MAIL_ENCRYPTION=tls

Step 4: Create Authentication


Laravel provide very quick way to create registration, login and forgot password with routes by auth command, So simply run bellow command to create:

php artisan ui vue --auth
npm install
npm run dev

Step 5: Run Migration


Next, we need to run migration command to generate tables in database. Open terminal and run this artisan command.

php artisan migrate

Step 6: routes/web.php


Change below code in web.php file.

Auth::routes();

to

Auth::routes(['verify'=>true]);

Step 6: app/Models/User.php


Open User.php from app/Models. And change or add below lines.

use Illuminate\Contracts\Auth\MustVerifyEmail;
class User extends Authenticatable implements MustVerifyEmail {

Step 7: app/Http/Controllers/HomeController.php


Open HomeController.php from app/Http/Controllers. And change __construct() function.

public function __construct() {
$this->middleware(['auth','verified']);
}


I really hope that I helped you understand email verification process, and if you don’t already use them I hope you now have an idea for what you can use them in your projects.

If you have any query or suggestions, feel free to ask me via the comment section below.

Leave a Reply