2024-08-05 23:31:15 +02:00
|
|
|
"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";
|
2024-08-05 23:31:15 +02:00
|
|
|
import { useSession } from "next-auth/react";
|
|
|
|
|
|
|
|
|
|
export const CSRFProvider: FC<PropsWithChildren> = ({ children }) => {
|
|
|
|
|
const { data: session, status, update } = useSession();
|
2024-08-14 10:51:12 +02:00
|
|
|
const router = useRouter();
|
2024-08-05 23:31:15 +02:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-08-14 13:03:50 +02:00
|
|
|
const checkSession = async () => {
|
2024-08-14 10:51:12 +02:00
|
|
|
console.log("Checking session status...");
|
|
|
|
|
console.log(status);
|
2024-08-05 23:31:15 +02:00
|
|
|
if (status === "authenticated") {
|
2024-08-14 13:03:50 +02:00
|
|
|
const response = await fetch("/api/v1/users/me", {
|
|
|
|
|
method: "GET",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (response.status !== 200 && !!router) {
|
2024-08-14 10:51:12 +02:00
|
|
|
console.log("redirecting");
|
2024-08-14 13:03:50 +02:00
|
|
|
window.location.href = "/auth/sso";
|
2024-08-14 10:51:12 +02:00
|
|
|
} else {
|
|
|
|
|
const token = response.headers.get("CSRF-Token");
|
2024-08-14 13:03:50 +02:00
|
|
|
update({ zammadCsrfToken: token });
|
2024-08-14 10:51:12 +02:00
|
|
|
}
|
2024-08-05 23:31:15 +02:00
|
|
|
}
|
2024-08-14 13:03:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const interval = setInterval(checkSession, 15000);
|
2024-08-05 23:31:15 +02:00
|
|
|
|
|
|
|
|
return () => clearInterval(interval);
|
2024-08-14 10:51:12 +02:00
|
|
|
}, [session, status, update, router]);
|
2024-08-05 23:31:15 +02:00
|
|
|
|
|
|
|
|
return children;
|
|
|
|
|
};
|