我已经像这样配置了令牌:
I've configured the token like this:
jwt.sign(
{
user: pick(user, ['_id', 'username'])
},
secret,
{
expiresIn: '2m'
}
);
但是当我想检查令牌是否过期时,这段代码不起作用
But when I want to check if the token was expired, this code doesn't work
function isAuthenticated() {
const token = localStorage.getItem('token');
const refreshToken = localStorage.getItem('refreshToken');
try {
decode(token);
const { exp } = decode(refreshToken);
if (exp < (new Date().getTime() + 1) / 1000) {
return false;
}
} catch (err) {
return false;
}
return true;
}
问题出在这部分:
if (exp < (new Date().getTime() + 1) / 1000) {
return false;
}
new Date().getTime() + 1)/1000 = 1531335468.113
new Date().getTime() + 1) / 1000 = 1531335468.113
exp = 1531334595
exp = 1531334595
因为不知道JWT用的是什么格式的时间...
Because I don't know what format of time uses JWT...
我该如何解决这个问题?
How can I resolve this?
谢谢!
如果有人想知道,这就是答案
This is the answer if someone want to know
if (Date.now() >= exp * 1000) {
return false;
}
这篇关于jwt 检查令牌是否过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!