Multi Auth with Roles and Permissions.

Step:-1 first of all you have to install xampp server use this url for download xampp: https://www.apachefriends.org/index.html

Step:-2 Download gitbash and install the gitbash use this url: https://git-scm.com/downloads

Step:-3 After install xampp serve see the pic how to start xampp server

XAMPP Control Panel with Start buttons for Apache and MySQL highlighted

Step:-4 follow this path for gitbash start. C:\xampp\htdocs or see this pic.

Windows File Explorer address bar showing the C:\xampp\htdocs path

Step:-4 In htdocs area click the right-click and start git bash here or see the pic.

Right-click menu in the htdocs folder with the Git Bash Here option selected

Step:-5 After Click on this Git Bash Here or see the pic

Git Bash window opened in /c/xampp/htdocs next to the htdocs folder in File Explorer

Step:6 Now you to have create one project whatever your project name (example:-hospital)

Step:-7 composer create –project –prefer-dist laravel/laravel hospital “5.5.*” after press Enter than it will take time for intalling your project. after install the project it will look like this.

Git Bash running composer create-project to install Laravel 5.5 as the hospital project

Step:-8 After install the project than you have to go inside project place(example: C:\xampp\htdocs\hospital) or see the pic

File Explorer at C:\xampp\htdocs\hospital showing the Laravel project folders and .env file

Step:-9 . Open your project in visual code or notepad++ or subline.

Step:-10 Go to .env file and put database name see the pic

.env file in VS Code with DB_DATABASE set to hospital_db, username root and an empty password

Step:-10 .Than go to this url: http://localhost/phpmyadmin/ and write your database name. or see the pic

PhpMyAdmin Databases tab creating a new database named hospital_db

Step:11– open your git bash command and write php artisan make:auth and press enter .or see the pic

Git Bash in the hospital project folder running php artisan make:auth
Git Bash running php artisan make:auth with Authentication scaffolding generated successfully

Step:12- Now you have to make controller php artisan make:controller Admin/AdminController than press enter or see the pic.

Git Bash with php artisan make:controller Admin/AdminController typed after make:auth succeeded
Git Bash output showing Admin/AdminController created successfully with php artisan make:controller

Step:-13– Make DashboardController for Admin Dashboard work (1) php artisan make:controller Admin/DashboardController (2) php artisan make:controller User/DashboardController

Step:-15 Now you have to go this path : Customise users table(database/migration/create_users_table.php) or see the pic

VS Code explorer steps to open database/migrations/create_users_table.php in the hospital project

Step:16– Now you have to go : database/migration/create_users_table.php than write this code:

Users table migration code with role_id, username, image and about columns highlighted

Step:-17 Make Model and Migration for Role Table. Run this code in git bash cammand. and press Enter. php artisan make:model Role -m

Git Bash running php artisan make:model Role -m in the hospital project
Output of php artisan make:model Role -m: model and create_roles_table migration created

Step:18 Customise roles table(database/migration/create_roles_table.php)

VS Code showing default create_roles_table migration with id and timestamps columns
Roles table migration in VS Code with name and slug string columns highlighted

Step:-19 In Role Model create users() Function put this code in Role model

public function users()
{
return $this->hasMany(‘App\User’);
}

Empty Role model class in app/Role.php opened in VS Code
Role model in VS Code with users() function returning hasMany App\User highlighted

Step:-20 In User Model create role() Function for relationship and put this code in User model

public function roles()
{
return $this->belongsTo(‘App\Role’);
}

User model in VS Code with roles() function returning belongsTo App\Role highlighted

Step:21 Make UsersTableSeeder file for insert data in users table through migration and run this code in your git bash cammand. php artisan make:seed UsersTableSeeder

Git Bash running php artisan make:seed UsersTableSeeder with Seeder created successfully

Step:22– Make RolesTableSeeder file for insert data in roles table through migration and run this code in your git bash cammand. php artisan make:seed RoleTableSeeder

Git Bash running php artisan make:seed RoleTableSeeder with Seeder created successfully

Step:23 Edit RoleTableSeeder.php(database/seeds/RoleTableSeeder.php) file for insert data in roles table put this code in the top use Illuminate\Support\Facades\DB;

RoleTableSeeder.php in VS Code with use Illuminate\Support\Facades\DB line highlighted

Step:24 put this code RoleTableSeeder.php(database/seeds/RoleTableSeeder.php

RoleTableSeeder run method inserting Admin and User roles with slugs into roles table

Step:25 Edit UserTableSeeder.php(database/seeds/UserTableSeeder.php) file for insert data in users table and put this code in top. use Illuminate\Support\Facades\DB;

UsersTableSeeder.php in VS Code with use Illuminate\Support\Facades\DB line highlighted

Step:26 Insert data in users table

UsersTableSeeder run method inserting admin and user accounts with role_id, email and bcrypt password

Step:26 Migration of tables in database php artisan migrate

Terminal running php artisan migrate, creating users, password_resets and roles tables

Step:27 Define UsersTableSeeder and RoleTableSeeder Class on DatabaseSeeder.php file
database/seeds/DatabaseSeeder.php and put this code

public function run()
{
$this->call(UsersTableSeeder::class);
$this->call(RoleTableSeeder::class);
}

VS Code DatabaseSeeder.php run method calling UsersTableSeeder and RoleTableSeeder

Step:28 php artisan db:seed (Insert data in tables)

Terminal running php artisan db:seed, seeding UsersTableSeeder and RoleTableSeeder

Step:29 Make AdminMiddleware for Admin Authentication Work. php artisan make:middleware AdminMiddleware

Terminal output of php artisan make:middleware AdminMiddleware: Middleware created successfully

Step:30 Make AuthorMiddleware for User Authentication Work. php artisan make:middleware UserMiddleware

Terminal creating AdminMiddleware and UserMiddleware with php artisan make:middleware

Step:31 Implement Condition for Admin Login in AdminMiddleware (app\Http\Middleware\AdminMiddleware.php) put this code in the top Use Auth;

VS Code AdminMiddleware.php with Use Auth statement added at the top

Step:32

public function handle($request, Closure $next)

{

    if(Auth::check() && Auth::user()->role->id == 1){

        return $next($request);

    }
 else {

        return redirect()->route('login');

    }


}Code language: PHP (php)
AdminMiddleware handle method allowing role id 1, otherwise redirecting to login

Step:33 Implement Condition for User Login in UserMiddleware (app\Http\Middleware\UserMiddleware.php) put this code in the top Use Auth;

public function handle($request, Closure $next)

{

    if(Auth::check() && Auth::user()->role->id == 2){

        return $next($request);

    }
 else {

        return redirect()->route('login');

    }
}Code language: PHP (php)
UserMiddleware handle method allowing role id 2, otherwise redirecting to login

Step:34 Implement Condition for User Login in RedirectIfAuthenticated (app\Http\Middleware\RedirectIfAuthenticated.php

RedirectIfAuthenticated handle method redirecting role 1 to admin dashboard and role 2 to user dashboard

Step:35 Implement Condition for Login in LoginController (app\Http\Controllers\Auth\LoginController.php)

protected $redirectTo;

LoginController.php with protected $redirectTo property highlighted

Step:36 Put this code in LoginController.php

LoginController constructor setting redirectTo by role to admin or user dashboard, marked Step 1 and 2

Step:37 Implement Condition for Login in ResetPasswordController (app\Http\Controllers\Auth\ResetPasswordController.php)

protected $redirectTo;

Step:38

public function __construct()

ResetPasswordController code with redirectTo property and role-based redirect in the constructor

Step:39 Define AdminMiddleware on routeMiddleware(path- app/Http/Kernal.php) use this code in the top use App\Http\Middleware\AdminMiddleware; App\Http\Middleware\UserMiddleware;

Kernel.php importing AdminMiddleware and UserMiddleware classes

Step:40 protected $routeMiddleware = [
‘admin’ => AdminMiddleware::class,
];

protected $routeMiddleware = [
‘user’ => UserMiddleware::class,
];

Kernel.php routeMiddleware array with admin and user middleware entries added

Step:41 Set middleware and route on web.php(Path routes/web.php)

a.Set Admin middleware and route

Step:42 .b.Set User middleware and route

Web.php route groups for admin and user dashboards using auth plus admin or user middleware

Step:43 you have create admin dashboard and user dashboard Make dashboard.blade.php file on resources/views/admin/admindashboard.blade.php Make dashboard.blade.php file on resources/views/user/userdashboard.blade.php

VS Code New Folder option under resources/views to create admin folder for admindashboard.blade.php
VS Code Explorer context menu on resources/views/admin with New File chosen to create admindashboard.blade.php
VS Code showing admindashboard.blade.php in the admin views folder with an h1 reading this is admin page

Step:44 . same you have to make for user

VS Code Explorer context menu on resources/views with New Folder chosen to create the user folder
VS Code context menu on the views/user folder with New File chosen to create userdashboard.blade.php
VS Code showing userdashboard.blade.php in the user views folder with an h1 reading this is user page

And finally you have run this code on your git base here cammand php artisan serve

Git Bash running php artisan serve, Laravel development server started at http://127.0.0.1:8000