import { FC, PropsWithChildren, useEffect } from "react";
import { CircularProgress } from "@mui/material";
import { useSession } from "next-auth/react";
import { useRouter } from "next/router";

export const Auth: FC<PropsWithChildren> = ({ children }) => {
  const router = useRouter();
  const { data: session, status: loading } = useSession();
  useEffect(() => {
    if (!session && !loading) {
      router.push("/login");
    }
  }, [session, loading, router]);

  if (loading) {
    return <CircularProgress />;
  }

  return <>{children}</>;
};
