且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel Passport密码授予-客户端身份验证失败

更新时间:2023-12-04 14:10:58

请首先检查您的凭据是否正确,其次检查使用\Laravel\Passport\HasApiTokens特征的模型表,其中是否包含email列,因为默认情况下用于在验证凭据时识别用户.如果您的表具有username列或用于验证凭据的任何其他列,则必须在该模型中定义一个函数findForPassport.像这样

Check your credentials first if they are correct, Secondly check your model table which uses \Laravel\Passport\HasApiTokens trait that whether it contains email column, because by default it is used to identify user when validating credentials. if your table has username column or any other column which is used in validating credentials you must define a function findForPassport in that model. like this,

public function findForPassport($username) {
       return self::where('username', $username)->first(); // change column name whatever you use in credentials
    }

我使用用户名和密码列来验证用户, 在{project_directory}\vendor\laravel\passport\src\Bridge\UserRepository.php

I use username and password column to validate a user, in {project_directory}\vendor\laravel\passport\src\Bridge\UserRepository.php

此功能验证您的凭据,

public function getUserEntityByUserCredentials($username, $password, $grantType, ClientEntityInterface $clientEntity)
    {
        if (is_null($model = config('auth.providers.users.model'))) {
            throw new RuntimeException('Unable to determine user model from configuration.');
        }

        if (method_exists($model, 'findForPassport')) { // if you define the method in that model it will grab it from there other wise use email as key 
            $user = (new $model)->findForPassport($username);
        } else {
            $user = (new $model)->where('email', $username)->first();
        }

        if (! $user || ! $this->hasher->check($password, $user->password)) {
            return;
        }

        return new User($user->getAuthIdentifier());
    }

注意第二个if语句,您将了解那里发生了什么.

notice the second if statement and you will get to know what is happening there.

希望获得帮助:)