Laravel 10 Create Multiple Authentication in Laravel - Taleem Dunya

Lecture 05

Laravel 10 Create Multiple Authentication in Laravel

Laravel indeed offers default authentication features such as login, registration, and password reset, and it even includes two-factor authentication with the Fortify package. However, it does not natively support multiple authentications. If you require multi-authentication in your Laravel application, you will need to implement it from the ground up.

In this Laravel 8/9/10 multi-auth tutorial, I will guide you through the process of creating multiple authentication methods in your Laravel application from scratch. This means that if you need to implement multi-authentication, you can do so without any concerns by following the steps I've outlined here.

While it's possible to create multiple authentications in Laravel using guards, this tutorial will take a different approach. We will use middleware to manage multi-authentication within the same user table, accommodating various roles. This method allows you to achieve multiple authentication scenarios without relying on distinct guards. Please refer to the preview of the Laravel multi-auth tutorial for more details.

Login as an admin - Step 1: Install Laravel

First of all, we need to get a fresh Laravel 9 version application using the bellow command, So open your terminal OR command prompt and run the bellow command to start laravel 9 multi auth create multiple authentication in laravel.


Step 2: Connect Database

Once you've completed the installation of your Laravel application and successfully configured the database settings, the next step is to navigate to the ".env" file. Within this file, you will need to make the necessary adjustments to the database name, username, and password to ensure they match your specific database setup.

.env


Step 3: Create Migration and Database Model

In this step, we need to add a new row "is_admin" in users table and model. then we need to run a migration. so let's change that on both files.

database/migrations/create_users_table.php


Update model as project requirement

Now update the user model by replacing it with the below code
 

app/Models/User.php


Step 5: Create Auth Scaffold

Here, we will use laravel ui package and create auth scaffold with the bootstrap framework. let's follow bellow command

 


Step 5: Create Middleware

In this step, we need to create admin middleware that will allow only admin access users to that routes. so let's create an admin user with the following steps in your app.


Create Route in Laravel

Here, we need to add one more route for the admin user home page so let's add that route in the web.php file.

routes/web.php


Create Controller in Laravel

Here, we need to add the adminHome() method for the admin route in HomeController. so let's add like as below.

app/Http/Controllers/HomeController.php


Create Blade file in Laravel 9.x/10.x

In this step, we need to create a new blade file for admin and update for user blade file. so let's change it.

resources/views/home.blade.php


Admin Blade code

Now create an admin home blade file and update it.

resources/views/adminHome.blade.php


Create LoginController

In this step, we will change on LoginController, when the user will login then we redirect according to user access. if a normal user then we will redirect to the home route and if admin user then we redirect to the admin route. so let's change.

app/Http/Controllers/Auth/LoginController.php


Finally run the server

Now you can test our application by visiting the below URL.

http://127.0.0.1:8000/