Laravel 8 Multi Authentication Example Tutorial

In this tutorial you will learn about the Laravel 8 Multi Authentication Example Tutorial and its application with practical example.

In this Laravel 8 Multi Authentication Example Tutorial I’ll show you how to create multi auth system using middleware in laravel 8. In Laravel Multiple authentication system multiple users can log in in one application according to their roles.

While developing a laravel application there comes situations where we want create separate user login or separate user/admin panel for users having different privileges or access rights in same application. In this laravel multi auth system, In this example we will create a middleware for checking the user’s role. It is an admin or normal user. We will create middleware name admin and configuration in the kernal.php file and also in the route file.

Laravel 8 Multi Authentication Example Tutorial

In this step by step Laravel 8 Multi Authentication Example we will learn to implement multi auth system using middleware in laravel 8.

  • Step 1: Install Laravel 8 App
  • Step 2: Connecting App to Database
  • Step 3: Setting up migration and model
  • Step 4: Create Middleware and Setting up
  • Step 5: Define Route
  • Step 6: Create Methods in Controller
  • Step 7: Create Blade View
  • Step 8: Start Development Server

Step 1: Install Laravel 8 App

First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command

Step 2: Connecting App to Database

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Step 3. Setting up migration and model

In this step we will add is_admin column in the users table using laravel mirgration. So, Open the creates_users_table.php migration file, which is placed on Database/migration and update the following field for admin.

Next open app/User.php and update the below field name is_admin here:


Now, in this step we will create model and migration file. Please run the following command:

Step 4: Install Laravel UI

Then install laravel 8 UI in your project using the below command:

Now, execute the below command on terminal for creating login, registration, forget password and reset password blade files:

Then execute the following commands:

Step 5: Create Middleware and Setting up

In this step we will create middleware for checking the user’s role. It is an admin or normal user.

Once the above command is executed a middleware is created. Noe go to app/Http/middleware and Implement the logic to check user role.


Now, we will register this middleware in the app/Http/Kernel.php. Now, open kernal.php and add the following $routeMiddleware property in it:

Step 5: Define Route

After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.

routes/web.php

Step 6: Create Methods in Controller

Now open the HomeController.php file, which is placed on app/Http/Controllers/ directory. Then add the following code into it:

Step 7: Create Blade View

Now, create two blade view files first is display home page and second is display after login. Open the resources/views/home.blade. file and update the below code.

Now, I checked the user profile. If it is admin, it will navigate to the admin area. Otherwise, it will redirect to users area. Create admin.blade.php file inside resources/views/ directory and update the following code:

Step 8: Run development server

Now we are ready to run our example so lets start the development server using following artisan command –

In this tutorial we have learn about the Laravel 8 Multi Authentication Example Tutorial and its application with practical example. I hope you will like this tutorial.