Backend modules from tastefully 8bd93d15: core-libs, core-session, api-gateway, mobile-api, services-auth, services-system, services-counter, services-member, services-log, services-email. solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal from tastefully 8bd93d15. Copied as tracked files only. Literal secrets replaced with environment placeholders: - services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY, MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default) - services-system: JUMIO_TOKEN, JUMIO_SECRET (optional) - services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed - core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default removed, ipgeolocation key read from IPGEOLOCATION_API_KEY - services-system EkycAppConfig: commented Jumio credentials removed Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
59 lines
2.2 KiB
TypeScript
59 lines
2.2 KiB
TypeScript
"use client";
|
|
|
|
import { loginUrl } from "@/app/lib/returnTo";
|
|
import { useEffect } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
|
|
// enforce-session-timeouts: keeps the server's view of this session current. Checks in on mount and once a minute;
|
|
// the first click, tap or key press after a minute without one is reported straight away, so activity near the idle
|
|
// limit is recorded promptly; later input rides on the next tick. Only
|
|
// real input counts as activity — background polling never does. A 401 means the session has ended (idle limit or
|
|
// cap): go to login. A network failure is ignored; the next tick tries again.
|
|
|
|
export const KEEPALIVE_INTERVAL_MS = 60_000;
|
|
const INPUT_EVENTS = ["pointerdown", "keydown"] as const;
|
|
|
|
export function SessionKeepAlive() {
|
|
const router = useRouter();
|
|
useEffect(() => {
|
|
let inputSinceLastCall = false;
|
|
// Last time activity was reported, so input near the idle limit is never held back by up to a minute.
|
|
let lastActiveReportAt = -Infinity;
|
|
let stopped = false;
|
|
|
|
const checkIn = async (active: boolean) => {
|
|
if (active) lastActiveReportAt = Date.now();
|
|
inputSinceLastCall = false;
|
|
try {
|
|
const res = await fetch("/api/auth/keepalive", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({ active }),
|
|
});
|
|
if (res.status === 401 && !stopped) router.replace(loginUrl(window.location.pathname + window.location.search));
|
|
} catch {
|
|
// Offline or a blip: keep the session; the next tick tries again.
|
|
}
|
|
};
|
|
|
|
const onInput = () => {
|
|
if (Date.now() - lastActiveReportAt >= KEEPALIVE_INTERVAL_MS) {
|
|
void checkIn(true);
|
|
} else {
|
|
inputSinceLastCall = true;
|
|
}
|
|
};
|
|
|
|
void checkIn(false);
|
|
const timer = setInterval(() => void checkIn(inputSinceLastCall), KEEPALIVE_INTERVAL_MS);
|
|
for (const e of INPUT_EVENTS) document.addEventListener(e, onInput, { capture: true, passive: true });
|
|
return () => {
|
|
stopped = true;
|
|
clearInterval(timer);
|
|
for (const e of INPUT_EVENTS) document.removeEventListener(e, onInput, { capture: true });
|
|
};
|
|
}, [router]);
|
|
|
|
return null;
|
|
}
|