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,
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,
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 ,
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.
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.
This tutorial helps integrate a PHP SDK with Laravel. We'll install aws-php-sdk into laravel application and access all aws services… Read More
in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More
This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More
We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More
in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More
I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More