How to Create Multiple Role Based Authentication in Laravel Application

Updated 19 May 2023

In this blog, we will go over the steps to create Multiple Role Based Authentication and Access Control in Laravel Application with a detailed explanation of the code.

Getting Started

We will start by installing a fresh new Laravel project to demonstrate

Step 1:

The following command can be used to install a fresh Laravel project, or you can check the documentation here.

Step 2:

Setting Databases

Next, let’s set up our database and configure our .env file to connect to our database properly

Step 3:

Now we need to customize the users_table under migration as per our requirement.

Now we have to create a model and migration for the role table where we can see the different roles that can login like ‘admin’, ‘user’ etc.

In the roles table, we will customize the Schemas as per the requirement.

Step 4:

So now we have two models named Users and Roles and both are dependent on each other hence we need to make a relationship between them.

To create a relationship we will create functions in both the model for the other one by giving the command given below.

Step 5:

In the next step, we need to create seeders for each of the models (User, Role). To create the UsersTableseeder and RolesTableSeeder

After the following step, we need to add insert function in the UsersTableseeder and RolesTableSeeder and also declare the use Illuminate\Support\Facades\DB; in both the seeders in the beginning since we will be using the DB class in the seeders.

Once done with the database creation we will migrate our tables into the database with the command given below.

In the next step we need to define two created seeders files (UsersTableSeeder and RolesTableSeeder) into the DatabaseSeeder.php as shown below:

Now we need to import our data stored in the Seeder files into the database which we created using the command:

Step 6:

Until here we have set up the database for storing the details which will be provided by the user and now we need to create the controllers and the middleware to authenticate and redirect the users to there respective pages.

First of all, we need to create a controller for the User and the Admin.

To create the controller we need to follow the command given below:

Now we will be creating middleware for the authentication of the Admin and the User.

Now we will add Auth trait in the AdminMiddleware and UserMiddleware for authentication of the User and the Admin by writing use Auth;

In the next step we will add handle() function in the AdminMiddleware and UserMiddleware.

We will also implement a Condition for Admin and User Login in RedirectIfAuthenticated which is in app/Http/Middleware/RedirectIfAuthenticated.php.

Step 7:

Now we need the default auth system to login and register users. Here, we will use laravel ui package and create auth scaffold with the Bootstrap framework. let’s follow bellow command:

Now create a simple bootstrap auth system:

Now we will be implementing a condition in the login controller before that we will remove ‘/home’ from protected $redirectTo = ‘/home’; since we don’t want to redirect the page to Home after logging in.

Likewise, we will implement the condition in the ResetPasswordController and remove ‘/home’ from protected $redirectTo = ‘/home’;

Step 8:

Once we are done with the above steps we will use AdminMiddleware and UserMiddleware in Kernal.php which is in app/Http/Kernel.php.

After that, we need to define and for defining we will add the command given below in routeMiddleware.

Now we are almost done with it and need to write the routes for the Admin and the User.

Almost done but we need to make the views as well where we can redirect the admin or user once they login.

-Create a blade file for the Admin dashboard under resource/views/admin/dashboard.blade.php.

-Create a blade file for the User dashboard under resource/views/user/dashboard.blade.php.

Under the dashboard blades of User and Admin, we can write the basic HTML to check whether it is showing it when redirected.

Thank you for reading my blog. If you have any queries you can free to contact me in the comment section.

Additionally, if you’re looking for custom solutions, you can hire Laravel developers, for your customised solutions. For exploring the available extensions for Bagisto, you can check out our extensions.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


2 comments

  • raju
    • Rishabh Yogi (Moderator)
  • Start a Project




      Message Sent!

      If you have more details or questions, you can reply to the received confirmation email.

      Back to Home