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.

 public function up()
	{
		Schema::create('users', function($table)
		{
			$table->increments('id');
			$table->string('email');
			$table->string('password');
			$table->text('permissions')->nullable();
			$table->boolean('activated')->default(0);
			$table->string('activation_code')->nullable();
			$table->timestamp('activated_at')->nullable();
			$table->timestamp('last_login')->nullable();
			$table->string('persist_code')->nullable();
			$table->string('reset_password_code')->nullable();
			$table->string('first_name')->nullable();
			$table->string('last_name')->nullable();
			$table->timestamps();

			// We'll need to ensure that MySQL uses the InnoDB engine to
			// support the indexes, other engines aren't affected.
			$table->engine = 'InnoDB';
			$table->unique('email');
			$table->index('activation_code');
			$table->index('reset_password_code');
		});
	}
 

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.

$params = array();

$params['code'] = $user->getActivationCode();
$params['email'] = $input['email'];
$params['url'] = url('user/activate').'?code='.$params['code'];

Mail::send('emails.user_activation', ['params' => $params], function ($message) use ($params) {
	
	$message->to($params['email'])->subject('Registration Activation Link');
});
//if not added, then add
$Request->session()->flash('activation_email', $input['email']);
 

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.

 public function activateAccount(Request $Request){
	$code = $Request->route('code');
	try
		{
			$user = Sentry::findUserByActivationCode($code);
			if ($user->attemptActivation($code))
			{
				// User activation passed
			}
			else
			{
				// User activation failed
			}
		}
		catch (Cartalyst\Sentry\Users\UserNotFoundException $e)
		{
			echo 'User was not found.';
		}
		catch (Cartalyst\Sentry\Users\UserAlreadyActivatedException $e)
		{
			echo 'User is already activated.';
		}
}
 

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.

Leave a Reply

Your email address will not be published.