mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 21:08:12 +00:00
51 lines
1.3 KiB
TypeScript
51 lines
1.3 KiB
TypeScript
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)}`);
|
|
}
|