Data Mastery
Data Mastery

Reputation: 2085

Converting a JWT with javascript results in a wrong expiration date

This is my JWT:

"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1OTgxMDM1MTcsIm5iZiI6MTU5ODEwMzUxNywianRpIjoiNzcxMjcyZjAtODQxNC00NDU5LTg5OGQtNzJiNGMzNGMyZGZjIiwiZXhwIjoxNTk4MTA0NDE3LCJpZGVudGl0eSI6Im1heHBvd2VyIiwiZnJlc2giOmZhbHNlLCJ0eXBlIjoiYWNjZXNzIiwidXNlcl9jbGFpbXMiOiJtYXhwb3dlciJ9.ilycgqpuyvnnHm63JPD9a9r090-Bu__uj2auEFnk3HA"

I get this from my Flask API and try to decode it like this:

const result = VueJwtDecode.decode(data.data.access_token);
console.log(result);
const expirationDate = result.exp;
console.log(expirationDate)

This results in a number (miliseconds?)

1598104755

I tried to convert this back to a normal Dateformat.

const expirationDate = new Date(result.exp)
console.log(expirationDate);

Mon Jan 19 1970 12:55:04 GMT+0100 (Mitteleuropäische Normalzeit)

This is obviously wrong. What am I doing wrong and why does this result in the year 1970?

Upvotes: 0

Views: 350

Answers (1)

CertainPerformance
CertainPerformance

Reputation: 370769

1598104755 is a number of seconds since the epoch in 1970.

Unlike some languages, JavaScript's Date is based on milliseconds, so you'll need to multiply by 1000 before passing to new Date:

console.log(
  new Date(
    1598104755 * 1000
  )
);

Upvotes: 3

Related Questions