Files
remark42/frontend/app/utils/jwt.ts
T
Pavel MineevandUmputun d22e364d1b Autosubmit token by paste and a bit more (#583)
* Use lib for typing tests instead copied types

* add jwt utils

* Add autosending

* fix

* tests

* remove atob and fix tests
2020-01-28 00:38:02 -06:00

19 lines
482 B
TypeScript

export const parseJwt = (token: string) => {
const [, base64Url] = token.split('.');
const base64 = base64Url.replace(/-/g, '+').replace(/_/g, '/');
const jsonPayload = decodeURIComponent(
atob(base64)
.split('')
.map(c => '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2))
.join('')
);
return JSON.parse(jsonPayload);
};
export const isJwtExpired = (token: string) => {
const { exp } = parseJwt(token);
return exp * 1000 < Date.now();
};