Using Role Based Authentication, Create Different Users DashBoard

Prerequisite:

i. Create a project in C:/xamp/htdocs directory by using Git Bash. To create a new project run the following code in Git Bash terminal

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

ii. Open the project in your code editor like VS Code

Step-1 : To use User Authentication in your project run the following code in code editor terminal

php artisan make:auth

Step-2 : Customize users table to add role field in the table. Go to database/migration/2021_03_20_000000_create_users_table.php and make some changes in users table.

Step-3: Create a Role Model and Migration for Role Table by using the following code:

php artisan make:model Role -m

Step-4: Now customize the Role table and add role_id and role_name field .Go to database/migration/2020_03_20_060108_create_roles_table and make some changes in up() function.

Step-5: Make Relationship between Users and Roles tables through Role Model and User Model.

i. To create relationship make changes in users( ) function in Role.php.

ii. In User.php make a role( ) function and write the following code .

Step-6 :  Create UsersTableSeeder  and RolesTableSeeder file by writting following two command in terminal.

php artisan make:seed UsersTableSeeder
php artisan make:seed RolesTableSeeder 

Step-7: Add Insert function in UsersTableSeeder.php and RolesTableSeeder.php file for Insert data in users table and role table. Add ” use Illuminate\Support\Facades\DB; ” in use area.

i. Go to database/seeds/UsersTableSeeder.php and add the following insert operation in run( ) function :

ii. Go to database/seeds/RolesTableSeeder.php and add the following insert operation in run( ) function :

Step-8 : Create a new Database and link it with the project

i. Run Apache and MySql .

ii. Open phpmyadmin/localhost in your web browser.

iii. Create a new database and set its name.

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

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=multipleuser (Your DB name)
DB_USERNAME=root
DB_PASSWORD=

v. Migrate it by following command:

php artisan migration

Step-9 : Insert data in the table

php artisan db:seed

Step-10: Create DashboardController for Admin Dashboard work and User Dashboard work.

i. For Admin Dashboard

php artisan make:controller Admin/DashboardController

ii. For User Dashboard

php artisan make:controller User/DashboardController

Step-11 : Make AdminMiddleware and UserMiddleware for Admin and User Authentication work.

i. For AdminMiddleware

php artisan make:middleware AdminMiddleware

ii. For UserMiddleware

php artisan make:middleware UserMiddleware

Step-12: Go to app/Http directory and Use Auth class for Authentication in AdminMiddleware.php and UserMiddleware.php in use section. Implement the following condition in handle( ) function:

i. In AdminMiddleware.php add the following codition:

i. In UserMiddleware.php add the following codition:

Step-12: Go to app/Http/Middleware/RedirectIfAuthenticated.php . Implement Condition for Admin and User Login in RedirectIfAuthenticated.php. and not forget to put ” use Auth; ” in use section.

Step-13: Go to app/Http/Controllers/Auth/ directory and remove = ‘/home’ from protected $redirectTo = ‘/home’; for ResetPasswordController.php and LoginController.php . Implement Condition Login in both php file in __construct( ) function.

i. In LoginController.php

ii. In ResetPasswordController.php

Step-14.A : Use AdminMiddleware and UserMiddleware in Kernel.php File in app/Http/Kernel.php directory

i. For AdminMiddleware

use App\Http\Middleware\AdminMiddleware;

ii. For UserMiddleware

use App\Http\Middleware\UserMiddleware;

Step-14.B : Define AdminMiddleware and UserMiddleware in routeMiddleware

i. For AdminMiddleware

'admin' => AdminMiddleware::class,

ii. For UserMiddleware

use App\Http\Middleware\UserMiddleware;

Tagged : /

Leave a Reply