···11+import React from 'react';
22+import { Navigate, useLocation } from 'react-router-dom';
33+import { useAuth } from '../contexts/AuthContext';
44+55+const ProtectedRoute = ({ children }) => {
66+ const { isAuthenticated, authLoading } = useAuth(); // Assuming 'authLoading' is the correct name from context
77+ const location = useLocation();
88+99+ if (authLoading) {
1010+ // Optional: Display a loading indicator while checking auth status
1111+ return <div>Loading authentication status...</div>;
1212+ }
1313+1414+ if (!isAuthenticated) {
1515+ // User not logged in, redirect to login page
1616+ // Preserve the original intended location in the state
1717+ return <Navigate to={`/login?returnUrl=${location.pathname}${location.search}`} replace />;
1818+ }
1919+2020+ // User is authenticated, render the child component
2121+ return children;
2222+};
2323+2424+export default ProtectedRoute;