Email Verification With Activation Token In Laravel 5 Using Sentry

I have already shared tutorial Login & registration in Laravel Using Sentry .I am extending this tutorial and add email verification system using activation code.

I am using sentry module for authorization so we will not add any extra method for generate activation code and database modification etc.We will add following steps into this tutorials,

  1. We will generate activation code using sentry method
  2. Send mail to user register email address
  3. Activate account on the click of activation mail link

I am assuming, you have read my previous tutorial and will add code into existing register method and other classes.This tutorial also help to create email verification process for custom register module.

The standard procedure of user registration and activation as as follow, I am following below steps from registration to activation of users,

  • User registers with email,first name, last name and password etc.
  • Laravel sends email with a random activation code to registered user email Id and also stored on the user row in database.
  • User will open email and clicked activation link that contains the activation token.
  • Laravel routes detect link and goes to action method, the method will find user information against activation code into database and set the user row to “1” and delete the activation key.
  • Send welcome email message.
  • After successfully activation, Redirect to user login page.

The sentry using following users table, please have a look for confirmation.

Where as ,

  1. activated : Varification bit, 1 for varified.
  2. activation_code : Activation code which will sent into mail.
  3. persist_code : This will help to prevent multiple user logged-in using same account.
  4. activated_at : Time-stamp when the email was verified.

If above column not exist you need to create above column into users table.

We will add below code into register method into UsersController.php file.

I have used sentry getActivationCode() method to generate activation code and send email to user input email id.

Ohhh, we have forget MAIL namespace into this controller UsersController.php file, now add below code on the top of the file.

use Mail;

SO now, when the user will register, an email will shoot on his given email id for varification.The next step will validate user verification and activate account based on activation code.

I have created mail activation link using action url 'user/activate' and activation code, so we will create new 'user/activate' into user controller file,

Added route entry into Http/routes.php file,

Route::get('user/activate', 'UserController@activateAccount');

As you can see, I have registered 'activate' action and bind with activateAccount method into UserController.php file.We will create method into in user controller file.

First we find user information based on activation code and passed to findUserByActivationCode() method to activate account, if user already activated, we are throwing message user is already activated.

Conclusion:

We have created mail using activation token using sentry method.I also sent the verification mail to user registered email id.The email verification process is using sentry module for authentication and activation.The user account will activate when the verification link be clicked from the email.