link-stack/apps/metamigo-frontend/components/Auth.tsx

21 lines
554 B
TypeScript
Raw Normal View History

2023-05-25 07:03:57 +00:00
import { FC, PropsWithChildren, useEffect } from "react";
import { CircularProgress } from "@mui/material";
2023-02-13 12:41:30 +00:00
import { useSession } from "next-auth/react";
import { useRouter } from "next/router";
2023-05-25 07:03:57 +00:00
export const Auth: FC<PropsWithChildren> = ({ children }) => {
2023-02-13 12:41:30 +00:00
const router = useRouter();
const { data: session, status: loading } = useSession();
useEffect(() => {
if (!session && !loading) {
router.push("/login");
}
2023-03-15 12:17:43 +00:00
}, [session, loading, router]);
2023-02-13 12:41:30 +00:00
if (loading) {
return <CircularProgress />;
}
return <>{children}</>;
};