91 lines
3 KiB
TypeScript
91 lines
3 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useRef } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
import { SESSION_EXPIRED_EVENT } from "@/hooks/useGameTick";
|
|
|
|
/**
|
|
* How early (ms) before the JWT `exp` we lock the client (dispatch the
|
|
* `SESSION_EXPIRED_EVENT`). The server is authoritative; this buffer only
|
|
* makes the client-side UX hint fire a little ahead of the hard cutoff so the
|
|
* user isn't mid-action when the token lapses.
|
|
*/
|
|
const NEAR_EXPIRY_BUFFER_MS = 30_000;
|
|
|
|
/**
|
|
* How often (ms) the safety check runs. It compares the wall clock against the
|
|
* server-provided `sessionExp` — it makes no server requests and reads no
|
|
* cookies. It exists to catch a throttled timer (e.g. a backgrounded tab) that
|
|
* delayed the one-shot timeout past the real expiry.
|
|
*/
|
|
const SAFETY_CHECK_INTERVAL_MS = 5 * 60_000;
|
|
|
|
/**
|
|
* Mounted only in the authenticated branch of `(frontend)/layout.tsx`.
|
|
*
|
|
* The server reads the HttpOnly `payload-token` cookie and passes only the JWT
|
|
* `exp` (ms) as `sessionExp`. This component schedules a one-shot lock timer
|
|
* just before expiry (dispatches `SESSION_EXPIRED_EVENT` so minigames and other
|
|
* consumers can lock themselves) and a refresh timer at the actual expiry
|
|
* (`router.refresh()` — the server then re-renders the layout, sees the expired
|
|
* token, and swaps the shell for the guest landing page). A lightweight safety
|
|
* interval catches throttled timers. Guards prevent duplicate lock/refresh.
|
|
*/
|
|
export function SessionWatchdog({ sessionExp }: { sessionExp: number | null }) {
|
|
const router = useRouter();
|
|
const lockFiredRef = useRef(false);
|
|
const refreshFiredRef = useRef(false);
|
|
|
|
useEffect(() => {
|
|
if (sessionExp === null) return;
|
|
|
|
const lock = () => {
|
|
if (lockFiredRef.current) return;
|
|
lockFiredRef.current = true;
|
|
window.dispatchEvent(new CustomEvent(SESSION_EXPIRED_EVENT));
|
|
};
|
|
|
|
const refresh = () => {
|
|
if (refreshFiredRef.current) return;
|
|
refreshFiredRef.current = true;
|
|
router.refresh();
|
|
};
|
|
|
|
const now = Date.now();
|
|
const lockDelay = sessionExp - NEAR_EXPIRY_BUFFER_MS - now;
|
|
const refreshDelay = sessionExp - now;
|
|
|
|
let lockTimer: number | null = null;
|
|
let refreshTimer: number | null = null;
|
|
|
|
if (refreshDelay <= 0) {
|
|
// Already past expiry — lock and refresh immediately.
|
|
lock();
|
|
refresh();
|
|
} else {
|
|
if (lockDelay <= 0) {
|
|
lock();
|
|
} else {
|
|
lockTimer = window.setTimeout(lock, lockDelay);
|
|
}
|
|
refreshTimer = window.setTimeout(refresh, refreshDelay);
|
|
}
|
|
|
|
// Safety net for throttled timers in background tabs.
|
|
const safety = window.setInterval(() => {
|
|
if (Date.now() >= sessionExp) {
|
|
lock();
|
|
refresh();
|
|
window.clearInterval(safety);
|
|
}
|
|
}, SAFETY_CHECK_INTERVAL_MS);
|
|
|
|
return () => {
|
|
if (lockTimer !== null) window.clearTimeout(lockTimer);
|
|
if (refreshTimer !== null) window.clearTimeout(refreshTimer);
|
|
window.clearInterval(safety);
|
|
};
|
|
}, [router, sessionExp]);
|
|
|
|
return null;
|
|
}
|