使用用户名而不是电子邮件进行身份验证 [英] Authentication on username instead of email
问题描述
我要检查用户名和密码,而不是电子邮件和密码.我发现这可行:
I want to check the user on username and password instead of email and password. And I found that this works:
public function login(){
if(!Auth::attempt(request()->only('username', 'password')))
{
return redirect('login');
}
return redirect('login');
}
但是我喜欢原始的login()
方法如何给出错误这些凭据与我们的记录不匹配".
But I liked how the original login()
method gave the error 'These credentials do not match our records.'.
我想知道这是否是"Laravel方式",因为我认为将其更改为用户名将是一行代码.如果这是正确的方法,我将如何重新显示该错误?
I would like to know if this is the "Laravel way" to doing this because I'd think it would be a one line code to change it to username. If this is the right way how would I get the error back to show up?
推荐答案
相反,我删除了登录方法,并将其添加到LoginController.php:
Instead of that I removed the login method and added this to LoginController.php:
/**
* Override the username method used to validate login
*
* @return string
*/
public function username()
{
return 'username';
}
这就是它的全部功能了!
That was all there is to it it works great!
这篇关于使用用户名而不是电子邮件进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!