api/jwt: return relative expiration date to accommodate offset clocks

This commit is contained in:
wukko 2024-08-19 22:25:21 +06:00
parent 1f3509db07
commit c698d272a1
No known key found for this signature in database
GPG key ID: 3E30B3F26C7B4AA2
2 changed files with 4 additions and 3 deletions

View file

@ -28,7 +28,7 @@ export const generate = () => {
return {
token: `${header}.${payload}.${signature}`,
exp,
exp: env.jwtLifetime - 2,
};
}

View file

@ -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;