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 TextAtom, 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:

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

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

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
{

}

That’s it! Now we can proceed to test the entire process from Registration to Email Verification.

Conclusion

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

4
1
CategoriesTags

Related Posts