[英]how to authenticate login in laravel using only username and password?
I have a mysql database of users with only 2 field: username and password.我有一个 mysql 用户数据库,只有 2 个字段:用户名和密码。 I'm trying to build a login page but the authentication function I'm using always returns false, even if the username and password are correct.我正在尝试构建一个登录页面,但我使用的身份验证功能总是返回 false,即使用户名和密码是正确的。
my if statement is:我的 if 语句是:
if (auth()->attempt($formFields))
where:在哪里:
$formFields = $request->validate([
'user' => 'required',
'password' => 'required'
]);
I tried to check if the problem is in the input i'm getting but it looks fine.我试图检查问题是否出在我得到的输入中,但它看起来很好。 from what i found online it appears that this function uses email field to authenticate so i think this might be the problem, but i couldn't find anyway around that...从我在网上发现的情况来看,这个功能似乎使用电子邮件字段进行身份验证,所以我认为这可能是问题所在,但我无论如何都找不到......
Try to update your LoginController and put following function there.尝试更新您的 LoginController 并将以下功能放在那里。
public function username()
{
$identity = request()->get('identity');
$fieldName = filter_var($identity, FILTER_VALIDATE_EMAIL) ? 'email' : 'user';
request()->merge([$fieldName => $identity]);
return $fieldName;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.