【发布时间】:2018-09-25 08:19:51
【问题描述】:
我正在尝试通过验证其他字段和表而不是用户表的电子邮件和密码来生成令牌。我正在使用tymon jwt 库。
我有三个字段需要验证以验证用户身份
table::where(["id"=>"1","mobile"=>"123","otp"=>"asdf"])->get();
因此,如果我在表中找到与此条件匹配的行,那么我想对用户进行身份验证并生成带有所需声明的有效令牌。
到目前为止我尝试过的是:
//after check for three fields in DB. If row matches then, $id and $contact are variable from DB.
$customClaims = ['id' => $id, 'mobile' => $contact];
$payload = JWTFactory::make($customClaims);
当我尝试这个时,我得到了JWT payload does not contain the required claims。
那么如何使用三个字段对用户进行身份验证,并生成具有所需声明和$customClaims 的有效令牌。
已编辑
public function verifyOTP(Request $request) {
$otp = $request->otp;
$schoolid = $request->schoolid;
$parent_contact = $request->contactNum;
$verifyOTP = OTP::where(['schoolid' => $schoolid, 'parent_numb' => $parent_contact, 'otp' => $otp])->get();
if ($verifyOTP) {
$customClaims = ['schoolid' => $schoolid, 'parent_numb' => $parent_contact];
$payload = JWTFactory::make($customClaims);
$token = JWTAuth::encode($payload);
return $token;
}
}
【问题讨论】:
-
可以分享所有相关代码吗?
-
@C2486 请参考编辑部分的完整代码。
-
您正在寻找的是
\Tymon\JWTAuth\Facades\JWTAuth::fromUser($user, $customClaims = []),其中$user是某种具有id字段的实体。您在这里遇到的错误是因为给定的声明不包含必需的声明 - 'iss'、'iat'、'exp'、'nbf'、'sub'、'jti'。您可以手动填写它们或使用给定的方法。 -
@GiedriusKiršys 如果您知道解决方案,为什么不写一个答案?
标签: laravel jwt lumen tymon-jwt