【问题标题】:Authentication with user role使用用户角色进行身份验证
【发布时间】:2018-05-25 15:26:51
【问题描述】:

我是 Laravel 的新手,在自定义身份验证系统时遇到了麻烦。我想让一个用户(来自表用户)使用user_id = 1(管理员)的列角色登录。我已经制作了一个名为 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 身份验证中的守卫?

【问题讨论】:

  • 希望this post by Medium能帮到你。
  • @o0o0keem 你能告诉我们错误吗?
  • @Miggy 当我尝试登录时它总是返回电子邮件或密码不正确

标签: php laravel authentication laravel-5


【解决方案1】:

我认为这将使您的代码正常工作。

改变:

$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
}

【讨论】:

  • 大声笑我不知道为什么狙击手会丢失它,但我的代码已经有了它,我对其进行了编辑。对不起,但这不是问题,如果我错过了 1 它会在 laravel 中显示错误
  • 你测试了代码,错误仍然存​​在吗?
  • 是的,电子邮件和密码仍然错误,即使我输入正确并且我检查了 user_id 是 1(来自表用户)
  • nvm mine doesn't work with role_id=0 的用户仍然可以登录
猜你喜欢
  • 2020-05-30
  • 1970-01-01
  • 2021-11-19
  • 2018-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多