【发布时间】:2020-01-26 04:32:48
【问题描述】:
在阅读了多个 JWT 刷新令牌教程后,我仍然不清楚 API 调用流程应该如何工作。
这是我的理解:
1) 客户端拥有访问令牌和刷新令牌。它将访问令牌提交给api/getCustomerData。
2) 假设访问令牌已过期。服务器回复401。
3) 客户端响应401 请求,将刷新令牌发送到api/token。
4) 服务器响应新的访问令牌,因为刷新令牌仍然有效。
5) 客户端随后使用新的访问令牌向api/getCustomerData 发出新请求。
我的印象是,这是过多的 API 调用,但我没有看到任何教程阐明了更有效地执行此操作的方法。就目前而言,如果我遵循这种模式,每个 API 请求将如下所示:
const getCustomers = async () => {
const config = {
data: body,
withCredentials: true,
method: 'POST' as 'POST',
}
await axios(address + '/api/getCustomerData', config)
.then((response) => {
...
})
.catch((error: any) => {
const response = error.response;
if (response) {
if (response.status === 401) {
if (!failcount){
failcount++;
getCustomers();
}
else {
history.push('/login')
}
}
}
})
}
【问题讨论】:
标签: authentication jwt axios