Adding Email Verification In laravel 5.3 App
adding email verification

Adding email verification process in your laravel 5.3 app step by step. First let’s initiate with an overview so we have an idea what is the actual process we are going to implement followed by the code and the steps.

Overview :

  • Create a user with a status of 0 (Zero) i.e. not verified initially
  • Store a random token with the user details on creation
  • Email the token to the user
  • Match the token & user id coming from mail with the database records
  • If matched set the user status to 1 i.e. verified

Now, lets start with the code & implementation step by step in a fresh installation of Laravel 5.3

NOTE – All code gists used contain proper comments to explain the code in the gist itself for the sake of keeping the steps short and informative as much as possible.

Install Laravel 5.3 and run php artisan make:auth

Migration changes for USERS table & model :

  • Edit the default migration for users table as follows
  • Run php artisan migrate
  • In app\User.php add the 2 new columns : ’email_token’ and  ‘verified’ to the fillable columns

RegisterController changes (Part 1) :

  • We will be using the following facades and files so make sure to use them at the top, that is , replace the top part with the following
  • Modify the CREATE method in it as follows
  • Over write the REGISTER method from the RegisterUsers trait by adding the following in the RegisterController

Getting Email ready :

  • Make sure to implement this laravel email configurations
  • Suggestion : We here at LUBUS use Mailtrap for email testing , it is easy & free to use
  • Run the followig artisan command
    • php artisan make:mail EmailVerification
  • It will create a new file “EmailVerification.php” in App/Mail directory
  • Modify it as follows
  • Create a new view in the resources/views/emails directory called “verification.blade.php”
  • Using the Laravel 5.3 default templates , we created this view for this email ( gist being too long would increase the scroll so linked it directly, sorry for that 🙂 )
  • Add the route
    • Route::get(‘register/verify/{token}’, ‘Auth\RegisterController@verify’); 
  • This route will be triggered from the confirmation button in the email

RegisterController changes (Part 2) :

  • Add the following code in it

User Model changes :

  • Add the following code in app/user.php

LoginController changes :

  • Add the use Illuminate\Http\Requestat the top
  • Override the credentials method from the authenticateUsers triat by adding the following in LoginController so that only verified users can login

That’s it. Try it , if you face any issues or get stuck somewhere with an error , comment below. We would be happy to help as quick as possible 🙂

If you want to save your time , you can directly download the already implemented boilerplate for the same :
Laravel 5.3 & 5.4 app with email verification process added