用户角色认证

时间:2017-12-12 01:55:45

标签: php laravel authentication laravel-5

我是Laravel的新手,我在定制身份验证系统方面遇到了麻烦。我希望用户(来自表用户)以user_id = 1(admin)的列角色登录。我已经制作了一个名为admin的中间件,而我的adminlogin功能似乎无法正常工作

public function postLogin(LoginRequest $request) {
    $admin = Auth::attempt(['email' => $request->email, 'password' => $request->password],'user_id'==1);

    if ($admin) {
        Auth::login(Auth::user()->user_id == 1, true);
        return redirect('/backend/dashboard');
    }
    return view('backend.auth.backendlogin', [
        'message' => "Email or password is incorrect",
    ]);
}

我是否还需要更改Laravel身份验证中的保护?

1 个答案:

答案 0 :(得分:0)

我认为这会使您的代码有效。

变化:

$admin = Auth::attempt(['email' => $request->email, 'password' => $request->password],'user_id'==1);
if ($admin) {
    Auth::login(Auth::user()->user_ ==1, true);
    return redirect('/backend/dashboard');
}

成:

$admin = Auth::attempt(['email' => $request->email, 'password' => $request->password],true);

if ($admin) {
    if(Auth::user()->user_id === 1){
        return redirect()->intended('/backend/dashboard');
    }
}else{
    #rest of the code
}