import keycloak from "Api/Keycloak/Keycloak";
import { useEffect, useRef } from "react";
import { Outlet } from "react-router";

export default function AuthGuard() {
  const loginStarted = useRef(false);

  useEffect(() => {
    if (!keycloak.authenticated && !loginStarted.current) {
      loginStarted.current = true;

      keycloak.login({
        redirectUri: window.location.href,
      });
    }
  }, []);

  if (!keycloak.authenticated) {
    return <div>Redirecting to login...</div>;
  }

  return <Outlet />;
}
