import React, { useContext } from "react"; import { Navigate } from "react-router-dom"; import { AuthContext } from "../context/AuthContext"; const AdminRoute = ({ children }) => { const { auth } = useContext(AuthContext); // Show loading state while checking authentication if (auth.loading) { return (
Loading...
); } // Redirect to login if not authenticated if (!auth.isAuthenticated) { return ; } // Redirect to home if not admin if (!auth.user?.isAdmin) { return ; } // Render the protected admin component return children; }; export default AdminRoute;