Laravel 5.6 — Customizing the default Auth (Part 1): Activate account after registration using Laravel Notification

Bijaya Prasad Kuikel
3 min readJan 12, 2018

--

Laravel provides the Basic Authentication out of the box which includes the basic login, registration, and password reset functionalities. These functionalities can be easily customized. This is the beauty of Laravel framework. In this tutorial we are going to customize the basic registration functionality of the Laravel and acount activation functionality after user registration.

So, let’s get started with a fresh installation of Laravel.

Let’s make some changes on the users table’s migration:

Next we need to generate the Auth Scaffolding provided by Laravel, using the following command on our terminal:

$ php artisan make:auth

Laravel generates Auth directory with following controllers.

Controllers Generated by Laravel

Laravel automatically updates ‘routes/web.php’ file and adds:

Auth::routes();

We can check the list the new routes with command

$ php artisan route:list
Routes List

We are interested in customizing the Registration system. So we will be updating the RegisterController.php. We are implementing Laravel Notifications so let’s create a new notification using the following command on the terminal:

php artisan make:notification UserRegisteredSuccessfully

This command generates a new notification class for us, inside the directory ‘App/Notifications’. Let’s edit the UserRegisteredSuccessfully.php

We need to define, a new route for validating / activating the account with named route ‘activate.user’ which is used in the Notification class (line no 55 above).

In our routes/web.php:

$this->get('/verify-user/{code}', 'Auth\RegisterController@activateUser')->name('activate.user');

Now, let’s edit our RegisterController , we will add activateUser method and also make changes in the user registration.

Here we are overriding the ‘register’ method of RegistersUser trait. After the user is registered we call the notify method and pass new instance of our UserRegisteredSuccessfully notification class which sends the notification mail to individual user with a link to activate the account.

Now, we need to update the fillable fields in our ‘User’ model.

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'username', 'password' , 'status', 'activation_code',
];

Also, we need to update our mail settings, for the testing purpose we can use Mailtrap.io. So we need to update the following keys in our .env file:

MAIL_HOST=smtp.mailtrap.io
MAIL_PORT=2525
MAIL_USERNAME=your_user_name
MAIL_PASSWORD=your_mail_password

Let’s edit our register.blade.php file to: add new username field and display the error message:

Let’s serve our application and then try to register a new user:

This sends a new mail to the user.

Mail for activation.

User can easily activate their account by clicking on the button.

User Logged in on activation.

Happy Coding :) Laravel Rocks !!

Buy me a coffee

--

--