User Registration and Login System in Laravel

Do you want to create a user registration and login system in Laravel? In most applications, you need to have a login, registration system. To access your application, the user must register with your website. However, building this flow requires handling a few features and should be built in a secure way to avoid any loopholes. It costs you a lot of time and energy.

The good news is you don’t need to create it from scratch. Laravel provides an official package that gives you ready-to-go flow for login and registration of the user.

The Laravel Breeze is a package that covers all authentication features required for a user registration system. Usually, the registration process involves the below steps.

  • Users fill up the sign up form.
  • A confirmation link will be sent to the user’s email address to validate their identity.
  • A user clicks on the activation link and gets activated for your website.
  • The user login to the system.
  • They can ask to reset their password if they forgot one.
  • A user can change their password.

All these points are covered in the Laravel Breeze package. It just saves a ton of time of yours from building all these features from scratch.

In this article, we study how to use the Breeze package to build the user registration and login system in Laravel.

Getting Started

For getting started, get ready with the Laravel project. If you don’t have one create it by running the command:

composer create-project laravel/laravel laravel-dev

I named the project laravel-dev. Install the Breeze package using the command:

composer require laravel/breeze --dev

After this, run the below command which scaffolds views, controllers, routes, and other resources required for authentication purposes.

php artisan breeze:install

Next, compile the assets through the below npm commands.

npm install
npm run dev

Finally, run the migrate command which will create tables in your database.

php artisan migrate

I’d recommend you check all the files created by the Breeze package. Some of them are under Controllers/Auth, views/auth, routes/, etc. 

You can now access the login and registration pages by navigating to /login or /register URLs in the browser. The sign-up page will display as the screenshot below.

register form

This one is the default design for the sign-up page. A user can change it by customizing the provided files say views/auth/register.blade.php. The Laravel Breeze’s view is made up of Blade templates and Tailwind CSS.

Apart from login and registration, you will also get a flow for the forgot password, confirm password and log out.

Email Verification

At this stage, users can register and log into the system. But the problem is they will not receive any confirmation link to activate their account. Let’s add this feature.

Open the App\Models\User model and implement the Illuminate\Contracts\Auth\MustVerifyEmail contract. Check out the code below.

App\Models\User.php

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable implements MustVerifyEmail
{
    use HasFactory, Notifiable;

    //...
}

This MustVerifyEmail interface will make sure to send an email with a verification link to the newly registered user.

To shoot your emails, you probably need to configure SMTP Server. For instance, to use the Gmail SMTP server you need to change the below settings on your Google account.

  • Login to your Google Account.
  • Click on ‘Account’ option.
  • On the ‘Account’ page, click on the ‘Security’ from the left sidebar.
  • Scroll down to the bottom and you will find ‘Less secure app access’ settings. Set it to ON.

Navigate to the .env file and update the SMTP credentials as follows.

MAIL_MAILER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
MAIL_USERNAME=GMAIL_USERNAME
MAIL_PASSWORD=GMAIL_PASSWORD
MAIL_ENCRYPTION=ssl

Now, when the user registers, they receive the verification email that looks something like the screenshot below.

verify-email-address

This email verification notification should fulfill your requirements. However, if you want to customize it then follow this link to get a better understanding of customization.

How Laravel Verifies the User?

The users table in the database contains the email_verified_at column. The newly registered user has a NULL value for this column. The NULL value stands for unverified users. Only after a user activates the account, this column is updated to the current timestamp value.

Protect Routes for Verified Users

In your application, you obviously required to restrict access to certain routes for only verified users. You can do it by adding verified middleware to such routes.

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

Here, the /profile URL will only be accessible to verified users. Other users will automatically redirect to the login page.

Conclusion

This tutorial will help you build a user login and registration system in Laravel. We showed you a Laravel Breeze package that simplifies building login and registration flow. You don’t need to worry about creating everything from scratch. Just follow the steps mentioned in the article and you will get the ready-to-go flow.

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

5 thoughts on “User Registration and Login System in Laravel

  1. There are so many assumptions made and dependencies missing here, that basically make this example unworkable. Most won’t even get beyond npm install, which will fail because of folder permissions.

  2. 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

  3. 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.