Laravel User Role based Access control Authentication – Technology

Laravel User Role based Access control Authentication – Technology

In this post we will give you information about Laravel User Role based Access control Authentication – Technology. Hear we will give you detail about Laravel User Role based Access control Authentication – TechnologyAnd how to use it also give you demo for it if it is necessary.

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.

Another must read:  PayPal Payment Gateway PHP Source Code

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

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 >
    <label for="role" >Role</label>
 
    <div >
        <select name="role"  >
            <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

Another must read:  Laravel GET POST PUT DELETE Methods

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

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

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' => 'AppHttpMiddlewareAdmin',
    'agent' => 'AppHttpMiddlewareAgent',
    'customer' => 'AppHttpMiddlewareCustomer',
];

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:

Another must 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 onlinecode 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.

Hope this code and post will helped you for implement Laravel User Role based Access control Authentication – Technology. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

7  +  2  =  

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US