fix(auth): prevent integer overflow in logout timer using safeTimeout (#5470)
parent
97b8911ba8
commit
dd883985bb
|
|
@ -91,3 +91,21 @@ export function createURL(endpoint: string, searchParams = {}): string {
|
||||||
|
|
||||||
return url.toString();
|
return url.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function setSafeTimeout(callback: () => void, delay: number): number {
|
||||||
|
const MAX_DELAY = 86_400_000;
|
||||||
|
let remaining = delay;
|
||||||
|
|
||||||
|
function scheduleNext(): number {
|
||||||
|
if (remaining <= MAX_DELAY) {
|
||||||
|
return window.setTimeout(callback, remaining);
|
||||||
|
} else {
|
||||||
|
return window.setTimeout(() => {
|
||||||
|
remaining -= MAX_DELAY;
|
||||||
|
scheduleNext();
|
||||||
|
}, MAX_DELAY);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return scheduleNext();
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import type { JwtPayload } from "jwt-decode";
|
||||||
import { jwtDecode } from "jwt-decode";
|
import { jwtDecode } from "jwt-decode";
|
||||||
import { baseURL, noAuth } from "./constants";
|
import { baseURL, noAuth } from "./constants";
|
||||||
import { StatusError } from "@/api/utils";
|
import { StatusError } from "@/api/utils";
|
||||||
|
import { setSafeTimeout } from "@/api/utils";
|
||||||
|
|
||||||
export function parseToken(token: string) {
|
export function parseToken(token: string) {
|
||||||
// falsy or malformed jwt will throw InvalidTokenError
|
// falsy or malformed jwt will throw InvalidTokenError
|
||||||
|
|
@ -22,10 +23,11 @@ export function parseToken(token: string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
const expiresAt = new Date(data.exp! * 1000);
|
const expiresAt = new Date(data.exp! * 1000);
|
||||||
|
const timeout = expiresAt.getTime() - Date.now();
|
||||||
authStore.setLogoutTimer(
|
authStore.setLogoutTimer(
|
||||||
window.setTimeout(() => {
|
setSafeTimeout(() => {
|
||||||
logout("inactivity");
|
logout("inactivity");
|
||||||
}, expiresAt.getTime() - Date.now())
|
}, timeout)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue