How to Implement Verify Email Feature In Laravel

How to Implement Verify Email Feature In Laravel

Each time I try to create a new working application in Laravel I often forget so many things when I want to implement a verify email feature, hence I thought it will be of help to put the entire process down in a tutorial such that the tech ecosystem will benefit from it.

INTRODUCTION

Laravel is an open source PHP framework intended for the development of web applications following the Model-View-Controller (MVC) architectural pattern.

PREREQUISITE

i. Local Server: XAMPP , WAMP , MAMP

ii . IDE / Code Editor: Sublime Text , Atom , VS Code

iii. Version Control System: Git

iv. Dependency Manager: Composer

GETTING STARTED

INSTALL LARAVEL

To install a Laravel application you need to open your Git bash or use a default terminal to enable you run commands during and after the installation process.

composer global require laravel/installer

Next, we need to create a fresh installation of laravel.

laravel new project_name

The installation process takes awhile to complete, but once its done we can navigate via the command into the just created Laravel project

cd project_name

Now we can proceed to generate a basic authentication scaffold. It includes: layout view, registration and login views, as well as routes for all user authentication.

composer require laravel/ui

php artisan ui vue --auth

SETTING UP THE DATABASE

To establish a database connection you need to edit connection parameters in the .env file of the project directory.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=your_database_name
DB_USERNAME=your_database_username
DB_PASSWORD=your_database_password

In this tutorial I’m working with XAMPP server and phpMyAdmin, so I show how to start the server with below screenshot:

xampp.PNG

Once the XAMPP server has started running on its port, proceed to create a database for the new project application.

your_database_name.PNG

It’s important to note that the name specified in .env for DB_DATABASE should be the name specified while creating the database.

CREATING THE DATABASE TABLE VIA MIGRATION

To create database tables in a Laravel application, we make use of migrations. In the database > migration lies a list of auto generated tables and also the ones we create via command line ourself. We will work with the ones auth command had earlier created for us, hence we just need to run a migrate command to push our table on the server

php artisan migrate

The above command runs all default Laravel migrations in our application provided you have created a matching DB_DATABASE which was created above.

Up next we can start our development server with another command.

php artisan serve

php artisan serve.PNG

laravel home.png

Now the Laravel welcome page gets rendered once the server has started running on 127.0.0.1:8080

ADDING MIDDLEWARE TO WEB ROUTES

Laravel ships with some default middlewares that can be used to prevent unauthourized access to certain resources in your application. In the app > Http > kernel.php you will find all available middlewares and you can also include your predefined middlewares once you find a need to create them. We will make use of the 'verified'in the protected $routeMiddleware array to verify if a user has truly verified the email sent to them by attaching the middleware to our dashboard route.

In the routes > web.php we will proceed to add two stuffs in there first we add 'verify=true' to the Auth and also attach a middleware ‘verified’ as shown below

Auth::routes(['verify' => true]);
Route::group(['middleware' => ['verified']], function(){

    Route::get('/dashboard', 'HomeController@show')->name('dashboard');

});

This will ensure that for anyone to be allowed to have access to the 'dashboard' URL they must have verified the email sent to their registered mailbox to be able to proceed.

UPDATING USER MODEL TO IMPLEMENT MUST-VERIFY-EMAIL

The final step to complete this process is to add implements MustVerifyEmailto the User class . In the app > User.php we need to update the model with the code below:

<?php

namespace App;

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


class User extends Authenticatable implements MustVerifyEmail
{

//.....

}

verify.PNG

CONCLUSION

Now you will be able to implement a verify email address feature into your Laravel application.