add 8 hours to the token issuance date

我的未来我决定 提交于 2021-01-28 19:16:14

问题


I have this function :

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;
};

In console.log() :

1516239022000
1585764070793

I have to check is token valid from issued at (iat)+8 hours till now. How can i add 8 hours to iat value.


回答1:


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 defines the numeric date:

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 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.



来源:https://stackoverflow.com/questions/60977764/add-8-hours-to-the-token-issuance-date

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!