php - 使用 Fortify Laravel 8 使用电子邮件或用户名登录
问题描述
我正在尝试允许用户使用电子邮件或用户名及其密码登录。
我在 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;
}
});
但是现在当用户尝试通过输入他们的用户名而不是电子邮件来登录时,他们会收到以下错误消息:
These credentials do not match our records.
解决方案
我忘记更改以下行:
->orWhere('username', $request->username)
到:
->orWhere('username', $request->email)
因为我的登录表单中只有一个字段可以保存电子邮件或用户名。此字段的名称是“电子邮件”。
推荐阅读
- stripe-payments - Stripe 处理 customer.subscription.deleted
- python - 分配前引用的页面
- cuda - CUDA 块级共享寄存器
- python - 为什么即使在安装 django-geoposition 之后我的地理定位也不起作用
- java - 在 H2 内存 DB 和 Spring boot @Entity 类之间存储和检索 JSON 类型数据
- node.js - 如何在反应中隐藏数据库信息?
- jenkins - 如何在 jenkinsfile 中定义触发源
- windows - 在 Windows 上的 Jenkins 容器中构建 maven 项目时找不到 Docker
- python - Pybind11 绑定访问 Gst.Buffer
- python - Python 3.7 中带有列表初始化的嵌套字典