"use client";

import { FC, PropsWithChildren, useEffect } from "react";
import { useSession } from "next-auth/react";

export const CSRFProvider: FC<PropsWithChildren> = ({ children }) => {
  const { data: session, status, update } = useSession();

  useEffect(() => {
    const interval = setInterval(async () => {
      if (status === "authenticated") {
        const response = await fetch("/api/v1/users/me");
        const token = response.headers.get("CSRF-Token");
        update({ csrfToken: token });
        console.log({ token });
      }
    }, 30000);

    return () => clearInterval(interval);
  }, [session, status, update]);

  return children;
};
