简体   繁体   中英

How to allow role based login in Laravel 5.4?

Using the function below I was able to add active flag, so that only those users who are active can login.

Now I have another flag called role and I wish to allow only those users with role 1 or 2 to login.

public function credentials(Request $request)
{
    return [
        'email' => $request->email,
        'password' => $request->password,
        'active' => 1,
    ];
}

You may like to use middleware

<?php

namespace App\Http\Middleware;

use Closure;

class CheckRole
{
    /**
     * Handle the incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string  $role
     * @return mixed
     */
    public function handle($request, Closure $next, $role)
    {
        if (! $request->user()->hasRole($role)) {
            // Redirect...
        }

        return $next($request);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM