JWT token exp is in weird format when making an API call

Viewed 942

I am trying to convert the JWT exp time to a date format. The weird thing is if I use google chrome and look at local storage, I get this format: 1594663193098

But when I use node to make an API call with the function below I get this: 21599

Trying to convert the second one to a regular date I get 1970, unless I do something like new Date(21599 * 73830000) which returns approx the correct time, I know it expires in 12h or 24h, not sure.

using the token from chrome i get this result new Date(1594663193098 * 1000) = +052502-11-12T22:04:58.000Z

const refreshToken = async () => {
  const requestBody = {
    grant_type: "refresh_token",
    client_id,
    client_secret,
    refresh_token
  };

  const config = {
    headers: {
      "Content-Type": "application/x-www-form-urlencoded"
    }
  };

  const url = "https://www.externalapiurl/api/oauth2/token";

  return axios
    .post(url, qs.stringify(requestBody), config)
    .then(response => response.data)
    .catch(error => console.log(error));
};

Why do I get a different exp format with node and how can i get the correct exp date from it?

1 Answers

The 21599(seconds) just tells you the token will expire in 6h, doesn't give you the actual date. So i just create a timestamp from the moment i get the token and add 6h to it.

timeNow = new Date()
timeNow.setSeconds(timeNow.getSeconds() + 21599);
Related