Laravel 5.8 Multiple Authentication Using Middleware

In this tutorial you will learn about the Laravel 5.8 Multiple Authentication Using Middleware and its application with practical example.

Laravel 5.8 Multiple Authentication Using Middleware

In this Laravel multi (auth) authentication tutorial we will learn how to create separate admin panel or login using middleware. Laravel multi (auth) authentication system allows to create multiple users login in single application.

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 laravel this can be achieved in following ways –

1. Laravel multi (auth) authentication using middleware

2. Laravel multi (auth) authentication Using Guards

In one of my previous article we have created separate admin login using laravel multi (auth) authentication using Guards. In this article we will creating separate admin login using laravel multi (auth) authentication system with middleware.

Install Laravel 5.8

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

Configure Database In .env file

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.

Generate Application Key

Open terminal and switch to the project directory and run the following command to generate application key and configure cache.

Set Default String Length

Locate the file “app/Providers/AppServiceProvider”, and add following line of code to the top of the file

and inside the boot method set a default string length as given below –

So this is how “app/Providers/AppServiceProvider” file looks like –

Migrations and User Model

After installing laravel, you will see the some migration files created inside database/migrations directory for creating default create users table and password reset table. Open app/database/create_users_table.php migration file and add/update the following field for admin.

After you added a column “is_admin” to user table run the migration using following command.

After this, go to app/User.php and add is_admin to fillable attribute in the User Model.

Authentication Scaffolding

Laravel comes with in-built basic authentication system, use the below command to generate default authentication scaffolding –

This command will generate required Controller files, views and add routes in our web.php routes file that are required for basic authentication system.

Create Admin Middleware

Now, we will create a middleware that authenticate users. This middleware will help to make sure who can access the admin area or who can access the normal user area. Use the following command to generate a middleware for Admin.

Next, open app/Http/ Middleware/Admin.php file, and here in handle() method implement the logic to authenticate users like below.

Now, we need to register admin route in $routeMiddleware property in the app/Http/Kernel.php file. Lets open app/Http/Kernel.php file and update the $routeMiddleware property like below –

Set Admin Protected Route

Next, create the route for admin. Open the routes/web.php file and add the following code in it.

routes/web.php

Create Controller Methods

Open the app/Http/Controllers/HomeController.php controller file and create two separate methods to handle admin and normal users.

app/Http/Controllers/HomeController.php

Create Blade/View

Now we will have two separate view files, first is for home page and second is for after login admin page. Open the resources/views/home.blade.php file and update the following code.

resources/views/home.blade.php

Here, if(auth()->user()->is_admin == 1) is used to check user is a normal user or an admin user, it will navigate to the admin area. Otherwise, it will redirect to users area.

Now, create a view file called admin.blade.php in the root of the views folder, and add the following code to it.

resources/views/admin.blade.php

Start Development Server

Start the development server using following artisan command –

In order to test laravel multi auth system first register a user through laravel register and then change the is_admin=1; and try to login again.

User Home 1 :-

laravel-multi-auth-2

After Login Admin Page :-

laravel-multi-auth-1

User Home 2 :-

laravel-multi-auth-3

In this tutorial we have learn about the Laravel 5.8 Multiple Authentication Using Middleware and its application with practical example. I hope you will like this tutorial.