Laravel Email Verification (version 5.7-7) Tutorial

0
298
Laravel Email Verification

Most of the web applications require customers to confirm their email addresses after signing up. Laravel provides convenient functionality for email verification. Laravel sends a verification link to the email address. Without confirming an email address with a verification link, you can not complete your signup process. Laravel email verification introduced in version 5.7. Please make sure you have at least laravel 5.7 or later versions.

# Installing Laravel

First of all, we need a Laravel installation. I’m using Laravel 7.* version throughout this tutorial.

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

# Authentication Scaffolding (Implementation)

Execute following commands to scaffold all of the routes and views you need for authentication.

composer require laravel/ui

php artisan ui vue --auth

After that, install npm packages and compile all UI modules using:

npm install

npm run dev

NOTE: Authentication implementation is different and easier if you are using Laravel (5.7, 5.8). You don’t need above commands. Use following command if you are using Laravel (5.7, 5.8).

php artisan make:auth

Next, start a development server by executing the following command. It will start a development server on localhost:8000.

php artisan serve

# Configuration

Open .env file situated in the project root folder and configure database and email SMTP in .env file. Email SMTP is necessary because we need to send a verification email to the customer. For email SMTP, I’m using mailtrap, which is a fake email server.

Database Configuration

/* .env */

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

Email Configuration

/* .env */

MAIL_DRIVER=smtp
MAIL_HOST=mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your-username
MAIL_PASSWORD=your-password
MAIL_ENCRYPTION=null

Keep in mind that, we must need email_verified_at column in the users table. Laravel include email_verified_at column in users table migration by default. Please verify your users table migration looks like below.

/* database > migrations > *_create_users_table.php */

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->id();
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->rememberToken();
        $table->timestamps();
    });
}

After, completing all configuration migrate the database with the following command.

php artisan migrate

# Setting Up Email Verification

First of all, open the User model and implements the Illuminate\Contracts\Auth\MustVerifyEmail interface. Laravel provides the VerificationController, which handles the required logic to send a verification email.

/* app > User.php */

<?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
{
    use Notifiable;

   /* .... */
}

Next, open web.php and set verify option to true in Auth::routes() function.

/* routes > web.php */

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

Finally, add verified middleware in web.php. Verified middleware prevents HTTP requests to visit routes without email verification. Of course, we can add middleware to the controller too. But it’s a good practice to handle middleware in routes files. I created a route group with a middleware array. Which contains auth and verified middleware, it will prevent unauthenticated and nonverified users simulatenously. Your web.php file looks like below.

/* routes > web.php */

<?php

use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('welcome');
});

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

Route::group(['middleware' => ['verified','auth'] ], function(){
    Route::get('/home', 'HomeController@index')->name('home');
});

After login laravel redirects you to /home. As you can see, we used auth middleware in the web.php, we don’t need to use in HomeController. So, remove auth middleware from __construct method.

/* app > Http > Controllers > HomeController.php */

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
{
    public function index()
    {
        return view('home');
    }
}

That’s it for laravel email verification. Now let’s test the application. Register a new user using http://localhost:8000/register URL.

Register A New User

After successful registration, you will be asked to verify your email address. At this point, you can verify with logout and login again. You can’t go further with verifying your email address.

Verify Email Address

Check, your email and click on Vefiry Email Address button.

Email Content

Now, try to login again you will login successfully.

Logged In User

Thankyou for reading this.

Learn more about Laravel Framework

LEAVE A REPLY

Please enter your comment!
Please enter your name here