import React, { useEffect } from 'react'; import { useLocation } from 'react-router-dom'; import { useAuth } from '../contexts/AuthContext'; const ProtectedRoute = ({ children }) => { const { isAuthenticated, loading } = useAuth(); const location = useLocation(); useEffect(() => { if (loading) return; if (!isAuthenticated) { const redirectUrl = `/login?returnUrl=${encodeURIComponent(location.pathname + location.search)}`; window.location.replace(redirectUrl); } }, [isAuthenticated, loading, location.pathname, location.search]); if (loading) { return
Checking authentication...
; } if (!isAuthenticated) { return
Not authenticated — redirecting to login...
; } return children; }; export default ProtectedRoute;