Email Verification tutorial in Laravel 5.7

Generic placeholder image
Brijpal Sharma
sql laravel  

Date:-September 20th, 2018 , Posted On:- Web Development


Email Verification tutorial in Laravel 5.7 is today topic. we discuss how to verify email in Laravel.

For avoiding fake user email verification is a good method. today we learn how to verify user email with Laravel 5.7.

To start first, we create a new Laravel project using this command 

 

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

 # or

 laravel new emailVerify

After successfully creating a project jump into the project folder.

cd emailVerify

And open this project in your favorite code editor. after that create a database and config .env file according to the database.

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

Step:1 Config .env file

Here use Laravel default migration file users so migrate it. using this flowing this command.

php artisan migrate

Email verification feature new in Laravel 5.7. so when user register and verify the email, the timestamp will store on the database so that we can easily differentiate which users email verify or not. before this Laravel new function mostly uses the method the datatype boolean. but now we use this new feature.

Step:2 Laravel 5.7 Auth Scaffolding

Now, open the terminal and run this following command.

php artisan make:auth

After that, it generates a new view file called verify.blade.php this is new in Laravel 5.7 for the verification.

@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  Edit User model for mustVerify interface

 

At the User.php model, we can see one more contract added called  MustVerifyEmail. To use the email verification process in our Laravel 5.7 project, 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',
    ];
}

 

Step:4 Config route in web.php file

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

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

 

This parameter enables the verification controller with the route action. we can notice that a new controller VerificationController.php file already comes with Laravel 5.7.

To protect any controller to verify just we need to add following code.

    public function __construct()
    {
        $this->middleware(['auth', 'verified']);
    }

 

Step:5 Setup email configuration

Here we use a Mailtrap (a fake SMTP server)for the testing purpose here is the link

Now add the config .env file for the Mailtrap.

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

 

Step:6 Test email verification functionality

To test email verification functionality in Laravel 5.7, go to the register page http://localhost:8000/register you will see this page

now to the mail trap and check your mailbox

When you click on this link your email will be verified and the user can access the protected controller.

So we completed Email Verification tutorial in Laravel 5.7 tutorial. 

 


About the author

user profile
Brijpal Sharma

Hello, My Name is Brijpal Sharma. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of Codermen. I started this blog to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers...

0 comments

You must be logged in to post a comment.