代币发行日期增加 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() 中:

1516239022000
1585764070793

我必须检查从 (iat)+8 小时颁发到现在的令牌是否有效。我怎样才能将 8 小时添加到 iat 值。

JWT 中的时间戳是从 1970 年 1 月 1 日开始计算的 UNIX 时间戳 00:00 UTC:https://www.rfc-editor.org/rfc/rfc7519#section-4.1.4 解释了数字日期用于 exp 声明(以及 nbf(不是之前)和 iat(发布于)索赔)

https://www.rfc-editor.org/rfc/rfc7519#section-2 定义数字日期:

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 添加到令牌 (1516239022) 的原始 iat 值。 但是,如您的代码所示,没有必要乘以 1000。