Laravel passport密码认证中自定义用户字段授权

一、博主版本

Laravel:5.4

php 7.0

二、过程

打开文件:\vendor\laravel\passport\src\Bridge\UserRepository.php

找到函数:getUserEntityByUserCredentials($username, $password, $grantType, ClientEntityInterface $clientEntity)

原函数内容如下:

public function getUserEntityByUserCredentials($username, $password, $grantType, ClientEntityInterface $clientEntity)
    {
        $provider = config('auth.guards.api.provider');

        if (is_null($model = config('auth.providers.'.$provider.'.model'))) {
            throw new RuntimeException('Unable to determine authentication model from configuration.');
        }

        if (method_exists($model, 'findForPassport')) {
            $user = (new $model)->findForPassport($username);
        } else {
            $user = (new $model)->where('email', $username)->first();
        }

可以看到,passport默认使用email字段作为用户信息验证字段。

若我想使用username字段作为用户信息验证字段,则修改为:

public function getUserEntityByUserCredentials($username, $password, $grantType, ClientEntityInterface $clientEntity)
    {
        $provider = config('auth.guards.api.provider');

        if (is_null($model = config('auth.providers.'.$provider.'.model'))) {
            throw new RuntimeException('Unable to determine authentication model from configuration.');
        }

        if (method_exists($model, 'findForPassport')) {
            $user = (new $model)->findForPassport($username);
        } else {
            $user = (new $model)->where('username', $username)->first();
        }

 

分享到:更多 ()