在代币发行日期上增加8小时 [英] add 8 hours to the token issuance date
问题描述
我有这个功能:
export const isTokenValid = () => {
const isTokenExist = localStorage.getItem("TOKEN_AUTH");
if (!isTokenExist) return false;
const token = isTokenExist.split(" ")[1];
const jwt = JSON.parse(atob(token.split(".")[1]));
const iat = (jwt && jwt.iat * 1000) || null;
console.log(iat);
console.log(Date.now());
const isExp = Date.now() > iat;
if (isExp) {
// localStorage.clear();
return false;
}
return true;
};
在console.log()中:
In console.log() :
1516239022000
1585764070793
我必须检查令牌的有效期是从(iat)+8小时到现在为止.我怎样才能将Iat值增加8个小时.
I have to check is token valid from issued at (iat)+8 hours till now. How can i add 8 hours to iat value.
推荐答案
JWT中的时间戳是UNIX时间戳,从1970年1月1日00:00 UTC开始计数:
The timestamps in JWT are UNIX timestamps counting from 01.01.1970 00:00 UTC: https://tools.ietf.org/html/rfc7519#section-4.1.4 explains that a numeric date is used for the exp claim (and also for the nbf (not before) and iat (issued at) claims)
https://tools.ietf.org/html/rfc7519#section-2 定义数字日期:
一个JSON数值,表示从 1970-01-01T00:00:00Z UTC直到指定的UTC日期/时间,忽略 秒.
A JSON numeric value representing the number of seconds from 1970-01-01T00:00:00Z UTC until the specified UTC date/time, ignoring leap seconds.
3600秒是一小时,因此您将8 * 3600 = 28.800添加到令牌的原始iat值(1516239022). 但是,正如您的代码所示,不需要乘以1000.
3600 seconds are one hour, so you add 8*3600 = 28.800 to the original iat value from the token (1516239022). But there's no need, as seen in your code, to multiply with 1000.
这篇关于在代币发行日期上增加8小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!