Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now

How to Create Multiple Role Based Authentication and Access Control in Laravel Application

Step1- Open command prompt or Git Bash on xampp/htdocs directory

Step2- Create Laravel New Project write this command

composer create-project --prefer-dist laravel/laravel devopsschool "5.8.*"

Step3- Move to project directory on git bash

cd devopsschool

Step4- For User Authentication write this command

php artisan make:auth

Step5- customize users table(database/migration/2014_10_12_000000_create_users_table.php)

Add Some columns in users table.

Step6- Create Model and Migration for Role Table

php artisan make:model Role -m

Step7- customize roles table(database/migration/2020_06_10_060108_create_roles_table)

Add Some columns in roles table.

Step8- Make Relationship between users and roles tables through Role Model and User Model.

Step8(A)- In Role Model Create users() function for relationship.

Step8(B)- In User Model Create roles() function for relationship.

Step9- Create UsersTableSeeder file for insert data in users table through migration.

php artisan make:seed UsersTableSeeder

Step10- Create RolesTableSeeder file for insert data in users table through migration.

php artisan make:seed RolesTableSeeder

Step11- Add Insert function in RolesTableSeeder.php(database/seeds/RolesTableSeeder.php) file for Insert data in roles table.

Step11(A)- use DB file in RolesTableSeeder.php

use Illuminate\Support\Facades\DB;

Step11(B)- Add Insert Function in run() function of RolesTableSeeder.php.

Step12– Add Insert function in UsersTableSeeder.php(database/seeds/UsersTableSeeder.php) file for Insert data in users table.

Step12(A) – use DB file in UsersTableSeeder.php

use Illuminate\Support\Facades\DB;

Step12(B)- Add Insert Function in run function of UsersTableSeeder.php.

Step13- Create Database in Mysql Server

Step14- Set Mysql Server Username ,Password and Database Name in .env file

Step15- Migration of Tables in Database

php artisan migration

Step16- Define UsersTableSeeder and RolesTableSeeder Class on DatabaseSeeder.php file(database/seeds/DatabaseSeeder.php)

Step17- Insert data in tables

php artisan db:seed

Step18- Create DashboardController for Admin Dashboard work.

php artisan make:controller Admin/DashboardController

Step19- Create DashboardController for User Dashboard work.

php artisan make:controller User/DashboardController

Step20- Make AdminMiddleware for Admin Authentication work

php artisan make:middleware AdminMiddleware

Step21- Make UserMiddleware for User Authentication work

php artisan make:middleware UserMiddleware

Step22- Implement condition for Admin login in AdminMiddleware(app/Http/AdminMiddleware.php)

Step22(A) – Use Auth class for Authentication in AdminMiddleware.php

use Auth;

Step22(B)- Implement Condition in handle() function of AdminMiddleware.php

Step23- Implement condition for User login in UserMiddleware(app/Http/UserMiddleware.php)

Step23(A)- Use Auth class for Authentication in UserMiddleware.php

use Auth;

Step23(B)- Implement Condition in handle() function of UserMiddleware.php

Step24- Implement Condition for Admin and User Login in RedirectIfAuthenticated(app/Http/Middleware/RedirectIfAuthenticated.php)

Step25- Implement Condition Login in LoginController(app/Http/Controllers/Auth/LoginController.php)

Step25(A)- remove = ‘/home’ from protected $redirectTo = ‘/home’;

Step25(B)- Implement Condition in __construct() function

Step26- Implement Condition Login in ResetPasswordController(app/Http/Controllers/Auth/ResetPasswordController.php)

Step26(A)- remove = ‘/home’ from protected $redirectTo = ‘/home’;

Step26(B)- Implement Condition in __construct() function

Step27- Define AdminMiddleware in routeMiddleware(app/Http/Kernel.php)

Step27(A)- use AdminMiddleware in Kernel.php File

use App\Http\Middleware\AdminMiddleware;

Step27(B)- Define AdminMiddleware in routeMiddleware

'admin' => AdminMiddleware::class,

Step28- Define UserMiddleware in routeMiddleware(app/Http/Kernel.php)

Step28(A)- use UserMiddleware in Kernel.php File

use App\Http\Middleware\UserMiddleware;

Step28(B)- Define UserMiddleware in routeMiddleware

'user' => UserMiddleware::class,

Step29- Set Middleware and Route in web.php (route/web.php)

Step29(A)- Set Admin Middleware and Route

Step29(B)- Set User Middleware and Route

Rajesh Kumar
Follow me