【发布时间】:2019-02-20 19:22:40
【问题描述】:
我有一个发出 JWT 令牌的端点:
/getJwt
token: {
"access_token": "kjhfdhglkfhjkjfhgkjfghdkjhgkjdsgfkjhdfkjgkjhfujksfknbvckjfhdbncvujkdhgvbnfjkdghnbv",
"expires_in": 2592000, // this is equal to 30 days - I dont have information when it is generated
"token_type": "Bearer"
}
我的应用程序有各种端点来获取数据,例如:
/getUser
/getbusinesses
等等……
所有这些端点都需要 JWT 才能访问。 如果 JWT 过期,我会收到以下回复:
{
"code": "401",
"message": "Unauthorized",
"errorList": [{
"code": "AUTH0007",
"message": "Malformed JWT token",
"fieldName": "Authorization"
}]
}
code:"AUTH0007" 对于 JWT 到期是唯一的。
我能否仅根据从上述端点返回的错误响应向getJwt 发出请求并重新提交失败的请求,还是需要让用户再次触发请求?
我愿意接受建议以实施任何更好的方法。
我成功地为每个请求调用 JWT - 我只想在它失败时进行调用。
简单的“获取请求”
axios.get('/user')
.then((result) => {
res.status(200).json(result.data);
})
.catch((err) => {
if (err.response.status === 401 && err.response.data.errorList.code === "AUTH0007") {
//utill function to get jwt token and update the token in code
// at this point can I send request back with updated token or do I need to ask user to trigger the new req
}
})
【问题讨论】:
标签: javascript node.js express jwt