User Registration And Login System In Laravel

Laravel provides built-in user registration and login system. Most of the developers are not aware of this built-in system (We also didn’t know this feature). When we come to know about this feature, we got surprised. It saves us a lot of time from building a login and registration system starting from scratch.

In this article, we study user registration and login system in Laravel – the feature provided by Laravel itself.

For getting started, we are assuming you have a fresh installation of a Laravel. If you don’t have it then create it by running the command:

composer create-project --prefer-dist laravel/laravel laravel-dev

Here ‘laravel-dev’ is the name of your Laravel project. Of course, you can change the name as you wish.

Head over to the project root directory in the terminal and run the command:

php artisan make:auth

This command will create authentication controllers like LoginController.php, RegisterController.php, etc which you will find the app/Http/Controllers/Auth directory. It also creates a views login.blade.php, register.blade.php under resources/view/auth directory.

This make:auth command also creates a app.blade.php file under resources/views/layouts directory. This view is a base layout for your application. It uses Bootstrap CSS framework but user can customize it and change the design.

Email Verification

While building a registration system, normally on signup we send an activation link to users. This activation link will use to verify the user account. Once, the user clicks on an activation link then we make that user active for our system. In other words, after verifying account user can browse the pages to our system.

Laravel provides a built-in system for email verification of a newly registered user. Using this, on registration user will get an email with activation link. Once he activated account then he would able to browse the system. Here, we can apply middleware('verified') for protecting routes. Doing so, these protected routes can be accessible only by verified accounts.

Open the App\User.php file and make sure this model implements Illuminate\Contracts\Auth\MustVerifyEmail contract.

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable implements MustVerifyEmail
{
    use Notifiable;

    .....
}

How does it work? If you check the migration file, user table must contain email_verified_at column. This column will use to verify whether the user activated their account. If activated, then this column will store date and time at the time of activation.

When we run the make:auth command, it also creates a Auth\VerificationController class which has logic written to send verification links and verify emails. To register the necessary routes for this controller, write the below routes in the routes/web.php file.

Auth::routes(['verify' => true]);

Next, to protect our routes from unverified account add middleware to it as follows:

Route::get('profile', function () {
    return '<h1>This is profile page</h1>';
})->middleware('verified');

You will have control of where to redirect the user after verification. If you open the Auth\VerificationController file you will find the variable $redirectTo which will use for redirection. Change this route as per your requirement.

protected $redirectTo = '/home';

Finally, run the migration command:

php artisan migrate

User Login and Registration

At this stage, we are ready to test the user login and registration system. For this to work, your application should able to send emails. You can use the Gmail SMTP server to send emails. For more details, please read our article Sending Email Via Gmail SMTP Server In Laravel.

Start the local development server using the command:

php artisan serve

Now, you should able to see your registration page at http://localhost:8000/register

register

Fill the form and you will get the verification link on your account as follows:

Email

Please note, Laravel allows us to login to our account even if we did not verify account yet. But we can’t access protected route. Remember we have added middleware for one of our route profile. Now without verifying your account if you try to visit the http://localhost:8000/profile, it will redirect to the http://localhost:8000/email/verify URL.

not verified

Go ahead and verify your account. You will see email_verified_at column in the users table has DateTime added. It means you have verified your account successfully. Now you should able to access even protected routes.

We hope you understand how to use user registration And login system in Laravel. You may also like to read our article Social Login System Using Laravel Socialite.

If you liked this article, then please subscribe to our Youtube Channel for video tutorials.

3 thoughts on “User Registration And Login System In Laravel

  1. Hey Sir, I am new to Laravel. I thank you first for this awesome tutorial however. I have tried to implement the same things in my laravel project. It shows no errors at all but the user isn’t registrated in my database. I verified my routes , the privileges and grants in database. The name of database . The .env file. And all other things are like you suggested. Please can you just give me a hint of what could be wrong? . Thank you

  2. In your user migration file, you have “activation_link” but in your User model it is “activation_key” in the fillable. This caused error when i tried it so i changed the migration data to “activation_key” Now I noticed the data saves into the database but i do not get any activation key or any message in my inbox. I changed the email to my email and also changed the smtp part in the env file..What do you think could be wrong. I get a 205 error

Leave a Reply

Your email address will not be published. Required fields are marked *