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

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Bijaya Prasad Kuikel
Bijaya Prasad Kuikel

Responses (31)

Write a response

great tutorial, I suggest you make it clear that you’re depending on the environment variable → ADMIN_MAIL

7

Nice tutorial, but am I missing something, the user can log in without clicking the activation link, authentication does not check the ‘status’ column at all.

7

Thank you, great guide!
Maybe it’s better to use the helper config(‘mail.from’) than env(‘ADMIN_MAIL’)
Laravel recommends use config(…) over env(…). I think that env is just recommended to use it in config files.

6