Laravel User Role based Access control Authentication

Today, We want to share with you Laravel User Role based Access control Authentication.In this post we will show you Laravel 5.7 role based access control, hear for Set-up role based access control in Laravel we will give you demo and example for implement.In this post, we will learn about User Role based Authentication and Access Control in Laravel with an example.

Laravel User Role based Access control Authentication

There are the Following The simple About Laravel User Role based Access control Authentication Full Information With Example and source code.

As I will cover this Post with live Working example to develop laravel roles and permissions, so the access control level in laravel for this example is following below.

roles namely Main Admin, Agent(Moderator), and Users Set up migrations:new role column

Also Read :  Browser window resize function with AngularJS

Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('email')->unique();
    $table->string('password');
    $table->string('role');// new add the role column
    $table->rememberToken();
    $table->timestamps();
});

Run the migrations using CMD

php artisan migrate

Step 2: Laravel the signup or registration Blade file on HTML form:

php artisan make:auth

resources/views/auth/register.blade.php

<div class="form-group row">
    <label for="role" class="col-md-4 col-form-label text-md-right">Role</label>
 
    <div class="col-md-6">
        <select name="role" class="form-control" >
            <option value="admin">Admin</option>
            <option value="agent">Agent</option>
            <option value="customer">Customer</option>
        </select> 
    </div>
</div>

Change The User Model

User.php

protected $fillable = [
    'name', 'email', 'password','role',
];

app/Http/Controllers/Auth/RegisterController.php

protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|string|max:200',
        'email' => 'required|string|email|max:200|unique:users',
        'password' => 'required|string|min:6|confirmed',
        'role' => 'required|in:admin,agent,customer', //Laravel validate role input
    ]);
}

create() simple method on Laravel Controller

protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'password' => bcrypt($data['password']),
        'role' => $data['role'],
    ]);
}

Step 3 :Laravel Set-up middlewares:

run this comands

php artisan make:middleware Admin
php artisan make:middleware Agent
php artisan make:middleware Customer

app/Http/Middleware/Admin.php

Also Read :  PHP MYSQLi Import Data From Excel to MYSQL

use Auth; //at the top
 
function handle($request, Closure $next)
{
    if (Auth::check() && Auth::user()->role == 'admin') {
        return $next($request);
    }
    elseif (Auth::check() && Auth::user()->role == 'agent') {
        return redirect('/agent');
    }
    else {
        return redirect('/customer');
    }
}

Agent.php:

use Auth; //at the top
 
function handle($request, Closure $next)
{
    if (Auth::check() && Auth::user()->role == 'agent') {
        return $next($request);
    }
    elseif (Auth::check() && Auth::user()->role == 'customer') {
        return redirect('/customer');
    }
    else {
        return redirect('/admin');
    }
}

Customer.php:

use Auth; //at the top
 
function handle($request, Closure $next)
{
    if (Auth::check() && Auth::user()->role == 'customer') {
        return $next($request);
    }
    elseif (Auth::check() && Auth::user()->role == 'agent') {
        return redirect('/agent');
    }
    else {
        return redirect('/admin');
    }
}

app/Http/Kernel.php $routeMiddleware

protected $routeMiddleware = [
    // ...
    'admin' => 'App\Http\Middleware\Admin',
    'agent' => 'App\Http\Middleware\Agent',
    'customer' => 'App\Http\Middleware\Customer',
];

routes/web.php:

Route::get('/admin', function(){
    echo "Hello Admin";
})->middleware('auth','admin');
 
Route::get('/agent', function(){
    echo "Hello Agent";
})->middleware('auth','agent');
 
Route::get('/customer', function(){
    echo "Hello Customer";
})->middleware('auth','customer');

controller’s constructor

public function __construct()
{
    $this->middleware('auth');    
    $this->middleware('admin');
}

Redirect User After Log-in:

Add this to your LoginController.php:

Also Read :  Laravel Roles Permissions Example Tutorial From Scratch

protected function redirectTo( ) {
    if (Auth::check() && Auth::user()->role == 'customer') {
        return redirect('/customer');
    }
    elseif (Auth::check() && Auth::user()->role == 'agent') {
        return redirect('/agent');
    }
    else {
        return redirect('/admin');
    }
}

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Laravel User Role based Access control Authentication.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.