使用电子邮件或用户名登录 Fortify Laravel 8

shb*_*789 7 php laravel laravel-authentication laravel-8 laravel-fortify

我试图允许用户使用电子邮件或用户名及其密码登录。

我在 FortifyServiceProvider.php 的 boot() 内添加了以下代码:

Fortify::authenticateUsing(function (Request $request) {
        $user = User::where('email', $request->email)
            ->orWhere('username', $request->username)
            ->first();

        if ($user &&
            Hash::check($request->password, $user->password)) {
            return $user;
        }
    });
Run Code Online (Sandbox Code Playgroud)

但现在,当用户尝试通过输入用户名而不是电子邮件登录时,他们会收到以下错误消息:

These credentials do not match our records.
Run Code Online (Sandbox Code Playgroud)

shb*_*789 6

我忘记更改以下行:

->orWhere('username', $request->username)
Run Code Online (Sandbox Code Playgroud)

到:

->orWhere('username', $request->email)
Run Code Online (Sandbox Code Playgroud)

因为我的登录表单中只有一个字段可以保存电子邮件或用户名。该字段的名称是“电子邮件”。