How to use Laravel Passport with a custom username column

你离开我真会死。 提交于 2019-11-26 19:57:35

问题


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


回答1:


You can use findForPassport method in your user model.

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();
    }
}



回答2:


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 code you'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.

Hope it helps someone.



来源:https://stackoverflow.com/questions/39194917/how-to-use-laravel-passport-with-a-custom-username-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!