Email verification in Laravel 5.8

By Author | May 08, 2019 | | 835 Views | Category : Laravel

This tutorial will be focused on verifying users email address before giving permission to access dashboard or any other features which are only accessible for Authorized people. To start with it, we will first create our Auth route and classes then enable inbuilt laravel functionality to authenticate user. You just need to make some basic setup with need to use middleware, routes and mail configuration.
Please follow each step as presented and you can easily implement Authentication in your current or existing project.

Step: 1  Create a new project of Laravel 5.8

Type the below command in console to create a new laravel project with latest version.

composer create-project --prefer-dist laravel/laravel email-verification
# OR
laravel new email-verification

Now once your project is ready we can configure some setting related to database and  Email.

Step: 2  Change configurations for DB and Email

In your .evn file you can override default values for database by below variables.







Now we also need to override Email configuration values in order to send email from our server in this case (eg. Gmail).






Now your Project is all set with your active mailing service.

Step: 3  Create Auth files for Laravel

In order to send registration Emails we first need to setup Authorisation setup where user can login and register and can visit dashboard only after successful Authentication. For this Laravel provides inbuilt Console command which will create all controllers, middleware, route, views etc, which are responsible for Authentication process.
Open your console/cmd and type the below command from root of your laravel project.

php artisan make:auth

Once you enter the above commnd you will get a message like "Authentication scaffolding generated successfully." if successful. And you can check controllers in auth folder should be created successfully.

Step: 4 Create database tables

Now we will run below command which will run "database/migrations/" classes to create user's table and password reset table.
NOTE: Before running migrate command plz create schema beforehand with same name as provided in .env file.

php artisan migrate

Step: 5 Model setup

Now we will implement Auth classes to user model, so that user account is only accessible after it is authenticated.


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',

Step: 6 Route Setup

When we ran command to make AUTH, One route was automatically generated " Auth::routes() ", now we will add verify parameter to it.

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

Step: 7 Controller setup

Now in home controller we will add a middleware "verified" along with already present auth middleware as presented below


namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller
     * Create a new controller instance.
     * @return void
    public function __construct()

     * Show the application dashboard.
     * @return \Illuminate\Contracts\Support\Renderable
    public function index()
        return view('home');

So that now homeController middleware will only be accessible by authorised and verified user.

Step: 8 Register and then login as regular user</strong

On to right corner click on register button to redirect on register form.

You can see Laravel's autoGenerated register form below

Once you fill your details and register the form, you will get a message after laravel has sent you email with verify token on mail.

Your can see as laravel has added a new column to track verified user which is currently "NULL".

Now below image will show you the sample email which laravel has generated with Authorisation token embedded.

Once you click the "Verify Email Address" button, you will be redirected to your home page saying you are logged in.

And when you open you users table, you can now see that column named "email_verified_at" is now filled with time at which user has clicked the email button.

Hope you are also able to implement this email verification in your project with above steps.

Tags :