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

Image for post
Image for post

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.

Image for post
Image for post

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
Image for post
Image for post

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:

Image for post
Image for post

This sends a new mail to the user.

Image for post
Image for post

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

Image for post
Image for post

Happy Coding :) Laravel Rocks !!

Buy me a coffee

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store