Add admin login page and required logic
This commit is contained in:
27
components/ProtectedRoute.tsx
Normal file
27
components/ProtectedRoute.tsx
Normal file
@ -0,0 +1,27 @@
|
||||
"use client";
|
||||
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useEffect } from "react";
|
||||
import { useAuth } from "@/context/AuthContext";
|
||||
import LoadingSpinner from "./LoadingSpinner";
|
||||
|
||||
export default function ProtectedRoute({
|
||||
children,
|
||||
}: {
|
||||
children: React.ReactNode;
|
||||
}) {
|
||||
const { authState } = useAuth();
|
||||
const router = useRouter();
|
||||
|
||||
useEffect(() => {
|
||||
if (!authState.isLoading && !authState.isAuthenticated) {
|
||||
router.push("/login");
|
||||
}
|
||||
}, [authState.isAuthenticated, authState.isLoading, router]);
|
||||
|
||||
if (authState.isLoading) {
|
||||
return <LoadingSpinner />;
|
||||
}
|
||||
|
||||
return authState.isAuthenticated ? children : null;
|
||||
}
|
||||
Reference in New Issue
Block a user