Email Verification in Laravel 5.7

By | March 6, 2019

Hello, Coders !! This tutorial is about Email Verification in Laravel 5.7 in the registration process.

Laravel 5.7 provides convenient methods for sending and verifying email verification requests.

The first there will be two possibilities that you can start this development from scratch or you have already set up the project and you just need to integrate this feature only.

There will be a possibility that you starting from scratch. just install a fresh Laravel project. using this link Install Laravel.

This will help you to create a fresh Laravel setup from scratch.

If you have already set up the project. Then you can continue from the below step.

Step 1 : Edit user model

Here we are going to edit the existing model file.

Edit {root}/app/User.php file

Just Implement MustVerifyEmail in this file. for that, you have to add “implements MustVerifyEmail” in class and you can see the code now like below.

<?php

namespace App;

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

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

Now migrate the table using this command

php artisan migrate

After running above command in the terminal if the user table already exists. Therefore, only one attribute email_verified_at will migrate.

After this user will send hit button and will get an email in mailtrap. So, when a user will open an email and confirm the link from the email.

Then at that time timestamp will be stored in this field and in this way we can confirm the user activation with exact date and time or not.

Step 2 : Auth Scaffolding

Run this command in terminal

php artisan make:auth

This command will generate verify.blade.php file at {root}/resources/views/auth/verify.blade.php and it will look like this

@extends('layouts.app')

@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">{{ __('Verify Your Email Address') }}</div>

                <div class="card-body">
                    @if (session('resent'))
                        <div class="alert alert-success" role="alert">
                            {{ __('A fresh verification link has been sent to your email address.') }}
                        </div>
                    @endif

                    {{ __('Before proceeding, please check your email for a verification link.') }}
                    {{ __('If you did not receive the email') }}, <a href="{{ route('verification.resend') }}">{{ __('click here to request another') }}</a>.
                </div>
            </div>
        </div>
    </div>
</div>
@endsection

Step 3 : Add Email Route Verification

Next Go to the {root}/routes/web.php file and add the extra parameter inside Auth::routes().

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

This will enable the new Verification controller with the route actions.

You can see the new controller called VerificationController.php file already comes with Laravel 5.7.

Now open {root}/app/Http/Controllers/HomeController.php

and add this middleware

 /** HomeController.php
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware(['auth', 'verified']);
    }

Step 4 : Setup Email Configuration

We can use mailtrap for quick testing.

Now just open this link https://mailtrap.io/signin and sign up or log in. 

Go to the demo inbox and copy the credentials and Laravel to your .env file.


MAIL_DRIVER=smtp
MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null

Step 5 : Testing Email verification

Go to register http://localhost:8000/register and fill-up the form

After submission, you will see this page

Now, you can go the mailtrap a third-party site’s page and you can see there that you will get a verification e-mail.

After that just open an email and verify that link.

After click on it, your activation has done and you’ll be logged in successfully !!

Thank You !!

2 thoughts on “Email Verification in Laravel 5.7

Leave a Reply

Your email address will not be published. Required fields are marked *