link-stack/apps/link/app/_components/ZammadLoginProvider.tsx

37 lines
1 KiB
TypeScript
Raw Normal View History

"use client";
2024-08-14 13:03:50 +02:00
import { FC, PropsWithChildren, useEffect } from "react";
2024-08-14 10:51:12 +02:00
import { useRouter } from "next/navigation";
import { useSession } from "next-auth/react";
export const ZammadLoginProvider: FC<PropsWithChildren> = ({ children }) => {
const { data: session, status, update } = useSession();
2024-08-14 10:51:12 +02:00
const router = useRouter();
useEffect(() => {
2024-08-14 13:03:50 +02:00
const checkSession = async () => {
if (status === "authenticated") {
2024-08-14 13:03:50 +02:00
const response = await fetch("/api/v1/users/me", {
method: "GET",
2024-09-25 10:04:43 +02:00
headers: {
2024-12-13 16:37:20 +01:00
"X-Browser-Fingerprint": localStorage.getItem("fingerprint") || "",
2024-09-25 10:04:43 +02:00
},
2024-08-14 13:03:50 +02:00
});
if (response.status !== 200) {
2025-03-24 11:48:40 +01:00
window.location.href = "/link/login";
2024-10-09 12:21:06 +02:00
} else {
const token = response.headers.get("CSRF-Token");
update({ zammadCsrfToken: token });
2024-08-14 10:51:12 +02:00
}
}
2024-08-14 13:03:50 +02:00
};
const interval = setInterval(checkSession, 15000);
return () => clearInterval(interval);
2024-08-14 10:51:12 +02:00
}, [session, status, update, router]);
return children;
};