< go back to home

Laravel Authentication Series (Part 2): Custom Register And Login Setup

May 4, 2020

This is the second part of a series of related articles regarding different Laravel authentication methods. We will show how to create a custom email/password implementation of login and regsitration. Usually, the default setup as shown in part 1 is good enough for most use cases along with some flexibilities. So going for this custom route is for when you have a special scenario. We will however implement a simple login/register procedure as a demo to have a good comparison.

For the requirements, I recommend to fist setup using Docker which I did a write up here. This allows us to avoid environment based issues by having a simillar baseline. I've written this article using Laravel 7 but it should work on other versions above 5.

Welcome page for a fresh Laravel install.

Setup the database and "users" table using Laravel migration. The default Laravel users migration is enough for this demo.

The focus is first in registration. Create the register page by creating file "regsiter.blade.php" in "app/resources/views/". Write the code below. Notice that we will keep it simple for this demo.

app/resouces/views/register.blade.php

Create a single-action controller for displaying the register page using artisan command.

Enter the following code in ShowRegisterController.php.

app/Http/Controllers/ShowRegisterController.php

Add the register page route in the web route file.

app/routes/web.php

Now that the register page is done, we'll work on the backend register post. Start by creating the register controller using Artisan command.

Enter the code in RegisterController file as shown below. Refer to the inline comments for the explanations.

app/Http/Controllers/RegisterController.php

Register part is now completed. Before testing this though, it would be better to build the "dashboard" page which is accessible only to an authenticated user. Logout will also be placed there. Start by adding the lines below in route web file.

app/routes/web.php

It is ideal to have the routes in its controller and to have logout be in a POST request instead of GET. But we are simplifying everything to focus only in showing custom authentcation so we are not including those. Go ahead and test the registration by goint to your registration page in the browser e.g. http://localhost/registration.

Our next implementation is login. Start by creating the routes for displaying login page and the backend.

app/routes/web.php

Create the view file login.blade.php.

app/resouces/views/login.blade.php

Generate the "LoginController" through Artisan command.

Note: If you encounter an issue in running the command line, try to comment out first the post login route containing the LoginController.

Populate the controller file LoginController that was created with the following:

app/Http/Controllers/LoginController.php

I did not include validations and authentication fail messages as we are minimizing the codebase so that only the essential barebone implementations are shown. You can now try to login in the login page (e.g. http://localhost/login) using the values you put in the register part. That's it, you now hve a functioning custom login and register implementation.