mega-plant/components/auth/SessionLifecycle.tsx

18 lines
716 B
TypeScript

"use client";
import { useEffect } from "react";
export function SessionLifecycle() {
useEffect(() => {
const onPageShow = (event: PageTransitionEvent) => {
if (event.persisted) window.location.reload();
};
window.addEventListener("pageshow", onPageShow);
const channel = "BroadcastChannel" in window ? new BroadcastChannel("mega-auth") : null;
if (channel) channel.onmessage = (event) => {
if (event.data === "logout") window.location.replace(window.location.pathname==="/clock"||window.location.pathname.startsWith("/clock/")?"/clock/login":"/login");
};
return () => { window.removeEventListener("pageshow", onPageShow); channel?.close(); };
}, []);
return null;
}