无法在Laravel中进行身份验证

时间:2018-07-01 18:02:17

标签: php laravel

我最近在Laravel中遇到此错误以进行身份​​验证

  

传递给Illuminate \ Auth \ SessionGuard :: __ construct()的参数2必须   是Illuminate \ Contracts \ Auth \ UserProvider的实例,给定null,   呼入   G:\ xampp \ htdocs \ newrestaurantcrm \ vendor \ laravel \ framework \ src \ Illuminate \ Auth \ AuthManager.php

任何人都可以给我一个有关此错误的想法,为什么会发生此错误? 我在我的Auth\AuthController.php文件中使用以下代码进行身份验证

protected function login(Request $request) {
    $email = $request->email;
    $password = bcrypt($request->password);
    if (Auth::login(['email' => $email, 'password' => $password])) {
        return redirect()->intended('/admin/dashboard');
    }
}

2 个答案:

答案 0 :(得分:0)

在根据emailpassword对用户进行身份验证时,请使用Auth::attemptAuth::once用于单个请求)。当我们拥有用户实例并想要使用该用户实例登录时,我们将使用Auth::login。对于您的情况,使用Auth::attempt

public function login(Request $request) {
    $email = $request->email;
    $password = bcrypt($request->password);
    if (Auth::attempt(['email' => $email, 'password' => $password])) {
        return redirect()->intended('/admin/dashboard');
    }
}

详细信息:https://laravel.com/docs/5.6/authentication#included-authenticating

答案 1 :(得分:0)

将代码更改为

public function login(Request $request) {
    $email = $request->get('email');
    $password = $request->get('password');
    if (Auth::attempt(['email' => $email, 'password' => $password])) {
        return redirect()->intended('/admin/dashboard');
    }
}

我将protected更改为public,将Auth::login()更改为Auth::attempt()。如果您使用login,则实际上必须传递您要登录的User object。您无需加密密码即可通过尝试。为了简化这个过程,您可以编写

public function login(Request $request) {
    if (Auth::attempt($request->only('email', 'password'))) {
        return redirect()->intended('/admin/dashboard');
    }
}

这当然假定您的表单的字段emailpassword的名称正确,并且email表中的字段passwordusers也相同也是