From c698d272a1d4a1bedac23a940a9ec2801912750f Mon Sep 17 00:00:00 2001 From: wukko Date: Mon, 19 Aug 2024 22:25:21 +0600 Subject: [PATCH] api/jwt: return relative expiration date to accommodate offset clocks --- api/src/security/jwt.js | 2 +- web/src/lib/api/session.ts | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/api/src/security/jwt.js b/api/src/security/jwt.js index e920e444..251b936a 100644 --- a/api/src/security/jwt.js +++ b/api/src/security/jwt.js @@ -28,7 +28,7 @@ export const generate = () => { return { token: `${header}.${payload}.${signature}`, - exp, + exp: env.jwtLifetime - 2, }; } diff --git a/web/src/lib/api/session.ts b/web/src/lib/api/session.ts index 0e5c426a..77e99284 100644 --- a/web/src/lib/api/session.ts +++ b/web/src/lib/api/session.ts @@ -42,10 +42,10 @@ export const requestSession = async() => { } export const getSession = async () => { - const currentTime = Math.floor(new Date().getTime() / 1000); + const currentTime = () => Math.floor(new Date().getTime() / 1000); const cache = get(cachedSession); - if (cache?.token && cache?.exp - 2 > currentTime) { + if (cache?.token && cache?.exp - 2 > currentTime()) { return cache; } @@ -59,6 +59,7 @@ export const getSession = async () => { } as CobaltErrorResponse if (!("status" in newSession)) { + newSession.exp = currentTime() + newSession.exp; cachedSession.set(newSession); } return newSession;