| 1234567891011121314151617181920212223242526272829 |
- import { FC, useEffect } from "react";
- import { useRouter } from "next/router";
- import { useAuth } from "../context/UserContext";
- import Spinner from "./Spinner";
- const PrivatePage: FC<{ protectedRoutes: string[] }> = ({
- protectedRoutes,
- children,
- }) => {
- const { pathname, push } = useRouter();
- const { isAuthenticated } = useAuth();
- const pathIsProtected = protectedRoutes.includes(pathname);
- useEffect(() => {
- if (!isAuthenticated && pathIsProtected) {
- push("/");
- }
- }, [isAuthenticated, pathIsProtected, push]);
- if (!isAuthenticated && pathIsProtected) {
- return (
- <Spinner />
- );
- }
- return <>{children}</>;
- };
- export default PrivatePage;
|