import Cookies from "js-cookie"; import { toast } from "sonner"; import i18n from "@/i18n"; import { clearRememberMePreference } from "../utils/authPersistence"; const PUBLIC_PATHS = [ "/login", "/sign-up", "/forgot-password", "/reset-password", "/set-password", "/verify-otp", "/verification_page", "/callback", "/complaints", "/complaint-form", "/follow-complaint", ]; let isHandlingExpiry = false; const isOnPublicPath = (pathname: string): boolean => { if (pathname === "/") return true; return PUBLIC_PATHS.some( (p) => pathname === p || pathname.startsWith(`${p}/`), ); }; export function handleSessionExpiry(): void { if (typeof window === "undefined") return; if (isHandlingExpiry) return; isHandlingExpiry = true; Object.keys(Cookies.get()).forEach((name) => Cookies.remove(name)); clearRememberMePreference(); toast.error(i18n.t("msg.authError"), { description: i18n.t("msg.authErrorDescription"), duration: 4000, }); const { pathname, search } = window.location; if (isOnPublicPath(pathname)) { window.location.replace("/login"); return; } const from = `${pathname}${search}`; window.location.replace(`/login?from=${encodeURIComponent(from)}`); }