Login And Registration Email Verification in Laravel 8

Share this Article
Reading Time: 8 minutes
3,504 Views

After registration sending an email to registered user is very important if we are developing a secure application. Inside this article we will see the concept of Email verification in laravel 8 using Breeze authentication tutorial.

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.

  • Email verification in Laravel 8 with Jetstream & Livewire authentication, Click here.
  • Email verification in Laravel 8 with laravel/ui scaffolding, Click Here

While implementing email verification in laravel main focus is to write the routes, middleware. We will do these points –

  • Create Laravel 8 application with Breeze authentication (You can use any other, no issues)
  • We will see Model & Route configuration

Let’s get started.


Installation of Laravel Application

Laravel Installation can be done in two ways.

  • Laravel Installer
  • By using composer

Laravel Installer

To install Laravel via Laravel installer, we need to install it’s installer first. We need to make use of composer for that.

$ composer global require laravel/installer

This command will install laravel installer at system. This installation is at global scope, so you type command from any directory at terminal. To verify type the given command –

$ laravel

This command will open a command palette of Laravel Installer.

To create ad install laravel project in system,

$ laravel new blog

With the name of blog a laravel project will be created at your specified path.

By using composer

Alternatively, we can also install Laravel by Composer command create-project. If your system doesn’t has Composer Installed, Learn Composer Installation Steps. Here is the complete command to create a laravel project-

$ composer create-project --prefer-dist laravel/laravel blog

After following these steps we can install a Laravel application into system.

To start the development server of Laravel –

$ php artisan serve

This command outputs –

Starting Laravel development server: http://127.0.0.1:8000

Assuming laravel already installed at system.


Create Database & Connect

To create a database, either we can create via Manual tool of PhpMyadmin or by means of a mysql command.

CREATE DATABASE laravel_app;

To connect database with application, Open .env file from application root. Search for DB_ and update your details.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_app
DB_USERNAME=root
DB_PASSWORD=root

Laravel 8 Authentication with Breeze

We are using here Breeze to create application authentication section like login and registration.

Install Breeze

We need to Install laravel breeze package for the authentication. Open Laravel project to terminal and type the given command.

$ composer require laravel/breeze --dev

It will install breeze to application. Now, to install laravel breeze for simple auth scaffolding.

$ php artisan breeze:install

After this command need to run the command

$ npm install && npm run dev

It will generate CSS and JS compiled files for authentication system.

About complete details of Breeze authentication in Laravel 8, click here.


Run Migration

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

$ php artisan migrate

SMTP Configuration with Laravel 8

To configure SMTP details, open up the file .env from application root. We will pass mail driver as gmail server, mail host, mail port, mail username, mail password.

MAIL_MAILER=smtp
MAIL_HOST=smtp.gmail.com
MAIL_PORT=587
MAIL_USERNAME="mygmail@gmail.com"
MAIL_PASSWORD="mypassword"
MAIL_ENCRYPTION=tls
MAIL_FROM_ADDRESS="mygmail@gmail.com"
MAIL_FROM_NAME="Online Web Tutor"
  

Make sure you have enabled google security setting form your gmail. go to Google account and click on “Account”. Once you are on the “Account” page, click on “Security“. Scroll down to the bottom and you will find “Less secure app access” settings. Set as ON.

Click here to learn in detail about SMTP Configuration in Laravel 8.


Model Preparation

Open User.php from /app/Models.

Before getting started, verify that your App\Models\User model implements the Illuminate\Contracts\Auth\MustVerifyEmail contract.

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    // ...
}

Model file is not ready to catch data in email verification process steps.

Next, we need routes.


Breeze Authentication Routes

Breeze package provides convenient built-in services for sending and verifying email verification requests.

When we install breeze package we get auth.php inside /routes folder.

Internally this auth.php file linked with web.php file.

require DIR.'/auth.php';

We should see this line will be added into web.php once we install breeze auth.

Open auth.php,

This file contains all authentication scaffolding routes. Let’s see email verification routes

Email verification routes will be of three types –

  • Route which sends email to user to verify.
  • Route which changes the status when user clicks on verify email.
  • Route which resend the email verification mail.

Route #Send Email Verification

This route will send verification email to an email address.

//...

Route::get('/verify-email', [EmailVerificationPromptController::class, '__invoke'])
  ->middleware('auth')
  ->name('verification.notice');

Route #The Email Verification Handler

First route sends mail to user email. Next, when we click on the email verification email then it will verify the email and return to dashboard. Here is the route which handles all these things in a very smart manner.

//...

Route::get('/verify-email/{id}/{hash}', [VerifyEmailController::class, '__invoke'])
  ->middleware(['auth', 'signed', 'throttle:6,1'])
  ->name('verification.verify');

Route #Resending The Verification Email

In case if we don’t receive any verification email. Then we need to resend that. For resending the email verification to mail again, here is the route which will do.

//...

Route::post('/email/verification-notification', [EmailVerificationNotificationController::class, 'store'])
  ->middleware(['auth', 'throttle:6,1'])
  ->name('verification.send');

Add Protecting Layer To Routes

Next, we need to add layer of middleware which protects users from direct access to routes. Once we verify the email address then it allows to go and check routes.

Open web.php from /routes folder.

//...

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

verified is in-built route middleware available in laravel application. To see, open Kernel.php from /app/Http folder.

Search for $routeMiddleware.

//...

protected $routeMiddleware = [
  
   //...
  
   'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];

Application Testing

Open project to terminal and type the command to start development server

$ php artisan serve

URL – http://127.0.0.1:8000/

We will get Login and Register Link at landing page.

When we click on Register, we should get a register page. We need to fill information and then we hit Register. It will register user into database but verification pending.

We will get an email verification page, but when we register user automatically it send an email to provided email address.

Open email address, we will get an email verification mail. Simply we need to click and verify our email.

Once user will be verified then each protected routes will be open to use. email_verified_at column will be updated inside users table.

We hope this article helped you to learn about Email Verification in Laravel 8 Tutorial in a very detailed way.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Find More on Laravel 8 Articles here

Leave a Comment