如何使用带有自定义用户名列的 Laravel Passport
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39194917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use Laravel Passport with a custom username column
提问by tehcpu
Now I'm using something like that for authenticating the user on my base site:
现在我正在使用类似的东西来验证我的基本站点上的用户:
if (Auth::attempt($request->only(['id', 'password']))) {
//
}
How can I modify this code for using custom column as username? https://laravel.com/docs/5.3/passport#password-grant-tokens
如何修改此代码以使用自定义列作为用户名? https://laravel.com/docs/5.3/passport#password-grant-tokens
回答by Alexander
You can use findForPassport method in your user model.
您可以在用户模型中使用 findForPassport 方法。
This method take username as argument, and return user model
此方法以用户名作为参数,并返回用户模型
For example:
例如:
class User extends Authenticatable
{
use HasApiTokens, Notifiable;
// ... some code
public function findForPassport($username) {
return $this->where('id', $username)->first();
}
}
回答by freddieRV
A bit too late to answer, but I've been having a hard time trying to figure this out, so for the sake of completeness and to maybe help others in the future.
回答有点晚了,但我一直在努力解决这个问题,所以为了完整起见,将来可能会帮助其他人。
If you take a look to this part of passport's codeyou'll see that it also looks for a validateForPassportPasswordGrant
method, so in addition to Alexander's answer, that's how you can authenticate an user using custom fields.
如果您查看护照代码的这一部分,您会发现它还在寻找一种validateForPassportPasswordGrant
方法,因此除了 Alexander 的回答之外,您还可以使用自定义字段对用户进行身份验证。
Hope it helps someone.
希望它可以帮助某人。