Laravel Passport custom validation for any /oauth/token request

限于喜欢 提交于 2020-01-24 19:52:29

问题


I need to validate extra fields in my users table before i create the requested tokens, but i can't find a simple way to do it with Passport.

I find similar workarunds which returns a token using $user->createToken() but i can't find someone covering all default /oauth/token options like the refresh_token

Also i see Passport have some simple ways to customize the username column and the password validation but i think this not cover my neededs.

Update

Im not sure if this is the best solution but in Laravel 5.8 the passport package have this validateForPassportPasswordGrant() function which allow you to add some extra conditionals before allowing the authentication process to get completed.

class User extends Authenticatable
{

    public function validateForPassportPasswordGrant($password)
    {
        if ($this->active != true) {
            throw OAuthServerException::accessDenied('The account is not active');
        }

        return Hash::check($password, $this->password);
    }
}

回答1:


In your login method

if (Auth::attempt(['email' => $request->email, 'password' => $request->password, 'is_active' => 1, 'username' => $request->username])) {
   // create a token here
}


来源:https://stackoverflow.com/questions/56614651/laravel-passport-custom-validation-for-any-oauth-token-request

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