Giter Site home page Giter Site logo

multi-auth-with-roles-in-laravel-9's Introduction

All steps for create multiple auth role base This project in laravel 9 so should be PHP version 8

  1. First create project

composer create-project laravel/laravel multiauth

Make database and add in .ENV file

  1. Add the role field in user migration table.

  2. I have added the role field in migration default user migration schema

public function up() {

    Schema::create('users', function (Blueprint $table) {
    
		$table->increments('id');
        
        $table->string('name');
        
        $table->string('email')->unique();
        
        $table->integer('role');
        
        $table->timestamp('email_verified_at')->nullable();
        
        $table->string('password');
        
        $table->integer('status_id')->nullable();
        
        $table->rememberToken();
        
        $table->timestamps();
        
    });
    
    
    
}
  1. them migrate with command

php artisan migrate

composer require laravel/ui

php artisan ui bootstrap --auth

npm install

npm run dev

  1. I have created seeder and added dummy data with all roles

php artisan make:seeder CreateUsersSeeder

public function run()
{
    $user = [
        [
            'name' => 'Super Admin',
            'email' => '[email protected]',
            'role' => '1',
            'password' => bcrypt('123456'),
        ],
        [
            'name' => 'Admin',
            'email' => '[email protected]',
            'role' => '2',
            'password' => bcrypt('123456'),
        ],
		[
            'name' => 'Player',
            'email' => '[email protected]',
            'role' => '3',
            'password' => bcrypt('123456'),
        ],
		[
            'name' => 'Team',
            'email' => '[email protected]',
            'role' => '4',
            'password' => bcrypt('123456'),
        ],
		[
            'name' => 'Academy',
            'email' => '[email protected]',
            'role' => '5',
            'password' => bcrypt('123456'),
        ],
		[
            'name' => 'Scout',
            'email' => '[email protected]',
            'role' => '6',
            'password' => bcrypt('123456'),
        ],
    ];

    foreach ($user as $key => $value) {
        User::create($value);
    }
}

Note use the User model at top of seeder file and then run below command for seeding

php artisan db:seed --class=CreateUsersSeeder

  1. Here is the following roles according to role name, I have created contoller and middleware

  2. Super Admin

  3. Admin

  4. Player

  5. Team

  6. Academic

  7. Scout

  8. Controller create command


php artisan make:controller SuperadminController

php artisan make:controller AdminController

php artisan make:controller PlayerController

php artisan make:controller TeamController

php artisan make:controller AcademicController

php artisan make:controller ScoutController

In all controller make a function and return view as name ,So make view file in view folder like:-

a. superadmin.blade.php

b. admin.blade.php

c. player.blade.php

d. team.blade.php

Here is the screenshot

image

Here is the view file content for admin

@extends('layouts.app')

@section('content')

<div class="row justify-content-center">
    <div class="col-md-8">
        
        <div class="card">
            <div class="card-header">Dashboard</div>
            <div class="card-body">
                
                @if (session('status'))
                    <div class="alert alert-success" role="alert">
                        {{ session('status') }}
                    </div>
                @endif
                You are in ADMIN Dashboard!
                
            </div>
        </div>
        
    </div>
</div>

@endsection

=========================================

public function __construct()

{

    $this->middleware('auth');
    
}

public function index() {

    return view('admin');
    
}
  1. Middleware create command

php artisan make:middleware Superadmin

php artisan make:middleware Admin

php artisan make:middleware Player

php artisan make:middleware Team

php artisan make:middleware Academic

php artisan make:middleware Scout

In all middleware to check the users role and direct with following code

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

{

    return $next($request);

}

return redirect()->route('login');
  1. Now all middleware register in kernel.php (App\Http\kernel) file in $routeMiddleware array

'player' => \App\Http\Middleware\Player::class,

'admin' => \App\Http\Middleware\Admin::class,

'superadmin' => \App\Http\Middleware\SuperAdmin::class,

'scout' => \App\Http\Middleware\Scout::class,

'team' => \App\Http\Middleware\Team::class,

'academy' => \App\Http\Middleware\Academic::class,

Here is the screenshot

image

  1. Now make the route in web.php file

Route::get('/player', [App\Http\Controllers\PlayerController::class,'index'])->name('player')->middleware('player');

Route::get('/admin', [App\Http\Controllers\AdminController::class,'index'])->name('admin')->middleware('admin');

Route::get('/superadmin', [App\Http\Controllers\SuperadminController::class, 'index'])->name('superadmin')->middleware('superadmin');

Route::get('/scout', [App\Http\Controllers\ScoutController::class, 'index'])->name('scout')->middleware('scout');

Route::get('/team', [App\Http\Controllers\TeamController::class, 'index'])->name('team')->middleware('team');

Route::get('/academy', [App\Http\Controllers\AcademicController::class, 'index'])->name('academy')->middleware('academy');

Also in default Login controller add this code

protected $redirectTo;

public function redirectTo() {

switch(Auth::user()->role){

    case 2:
    
    $this->redirectTo = '/admin';
    
    return $this->redirectTo;
    
        break;
        
    case 4:
    
            $this->redirectTo = '/team';
            
        return $this->redirectTo;
        
        break;
        
    case 3:
        $this->redirectTo = '/player';
        
        return $this->redirectTo;
        
        break;
        
    case 5:
    
            $this->redirectTo = '/academy';
            
        return $this->redirectTo;
        
        break;
        
    case 6:
    
        $this->redirectTo = '/scout';
        
        return $this->redirectTo;
        
        break;
        
    case 1:
    
		$this->redirectTo = '/superadmin';
        
        return $this->redirectTo;
        
        break;
        
    default:
    
        $this->redirectTo = '/login';
        
        return $this->redirectTo;
        
}
 
// return $next($request);

}

image

Similar add in register controller

image

Note :- Login and Register url is default as auth default.

image

Here login and Register url:-

/login

/register

If you want to see database check the db folder.

Thank you

multi-auth-with-roles-in-laravel-9's People

Contributors

mdmuzaffer avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.