Laravel Passport自定义验证适用于任何/oauth/token请求 [英] Laravel Passport custom validation for any /oauth/token request
问题描述
在创建请求的令牌之前,我需要验证users
表中的其他字段,但是我找不到使用Passport
进行此操作的简单方法.
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
.
我发现类似的工作方式,使用$user->createToken()
返回令牌,但是我找不到覆盖所有默认/oauth/token
选项(例如refresh_token
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
我也看到Passport
有一些简单的方法可以自定义用户名列和密码验证,但是我认为这不能满足我的需要.
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.
更新
我不确定这是否是最好的解决方案,但是在Laravel 5.8
中,passport
软件包具有此validateForPassportPasswordGrant()
函数,可让您在完成身份验证过程之前添加一些额外的条件.
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);
}
}
推荐答案
在您的登录方法中
if (Auth::attempt(['email' => $request->email, 'password' => $request->password, 'is_active' => 1, 'username' => $request->username])) {
// create a token here
}
这篇关于Laravel Passport自定义验证适用于任何/oauth/token请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!