AppDividend
Latest Code Tutorials

Laravel 5.7 Email Verification Tutorial Example

2,495

Get real time updates directly on you device, subscribe now.

Laravel 5.7 Email Verification Tutorial Example From Scratch is today’s leading topic. In this version, you need to configure the settings and write some minimal code to setup everything. Email verification is the must functionality in web apps, and laravel makes very easy. So let us do it then.

Earn a Tech Degree and get the skills like Frontend Development or Javascript Development that can help you to launch a career. Join the program

Laravel 5.7 Email Verification Tutorial Example

First, install the Laravel 5.7 using the following command.

#1: Install Laravel 5.7 and configure the database.

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

# or

laravel new emailVerify

 

Laravel 5.7 Email Verification Tutorial Example

Go inside the folder.

cd emailVerify

Fire up your favorite IDE or Editor.

code .

Create the MySQL database and write the credentials inside the .env file.

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

Okay, now migrate the tables by the following command.

php artisan migrate

Now, see the users table and you can there is one more field called email_verified_at.

 

Laravel 5.7 Email Verification

The email_verified_at column is there which is new in Laravel 5.7. So when the user registers and verifies the email, the timestamp will be recorded here. So based on that, we can differentiate that user has confirmed the email or not. For this kind of functionality, We have generally used the datatype boolean, but nowadays people are using a timestamp to accomplish this kind of goal.

#2: Laravel 5.7 Auth Scaffolding

Okay, now go to the terminal and type the following command.

php artisan make:auth

This command has generated one more view called verify.blade.php. It is new in Laravel 5.7 as the verification functionality is implemented in this version.

@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

#3: Implement mustVerify interface in the User model.

In the User.php model, you can see one more contract added called  MustVerifyEmail. To use the email verification process, we need to implement this contract.

<?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',
    ];
}

#4: Add Email Route Verification

Go to the routes >> web.php file and add the extra parameter inside Auth::routes().

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

This enables the new Verification controller with the route actions. You can see the new controller called VerificationController.php file already comes with Laravel 5.7.

Also, we need to protect the HomeController route, so let us do that via adding middleware.

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

#5: Setup email configuration

I am using mailtrap for this example. So log in to the https://mailtrap.io/signin.

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

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

#6: Test the email verification functionality.

First, go to the browser and go to either http://localhost:8000/register or like me go to the http://emailverify.test/.register 

Related Posts
1 of 41

You will see the page like this.

 

Verify Email in Laravel

Now go the mailtrap, and you can see that verification mail has arrived.

 

Email Verification in Laravel 5.7

Also, see the database and analyze the users table.

 

Laravel 5.7 Auth Functionality

Here, the email_verified_at is null. Now, click the link that is arrived at your email and your email will be verified and you can see here the timestamp will be registered.

 

Email verified in Laravel

So, finally Laravel 5.7 Email Verification Tutorial Example is over. Thanks for taking.

5 Comments
  1. David Rusnac says

    How do you edit the view of the verification email. The make:auth command doesn’t install a email view but seems to be auto generated.

  2. Ronald says

    Hi, thanks for ” Laravel 5.7 Email Verification Tutorial Example ” but I got a big question, how can I customize the verification email message sent to the user?: “Hello! Please click the button below to verify your email address. ….”

    I heard that is has something to do with “public static $toMailCallback; ” in the VerificationController.php, but I don’t know how to do it.

    Thanks a lot.

  3. MIke says

    There is an error i got on home controller
    */
    public function __construct()
    {
    $this->middleware([‘auth’, ‘verified’]);
    }

    cant pass two argument at the same time

    1. Test says

      Look again at: $this->middleware([‘auth’, ‘verified’]);

      It should be $this->middleware([‘auth’ => ‘verified’]);

  4. Lee says

    Am I the only person who thinks checking for email verification on every request is a bad idea?

    On a secure site where most pages require you to login your more than likely going to want to check Auth and Verified, which means every request to every secure page on your site checks if the user verified their email. Seems crazy to me, another layer to go through every time even if it is a small hit.

    Surly it would make sense to just check this once at login as the default and allow you to use it as middleware if you have that requirement. Plus it feels wrong to have to check both Auth and Verified, I know they are testing different things I just feel verified should be part of Auth once its implemented.

    Maybe I’m missing soothing, but I don’t like it, and I hate having to develop my own version of something that’s bundled. I wish they had made it a composer package so I could choose to use it, or better still not made middleware the default.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.