Something went wrong. Try again.
perform your own verifications verifier.psingletary.com/verifier
Something went wrong. Try again.
824 B · 26 lines
JSX
at main
123456789101112131415161718192021222324252627import 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 returnUrl = `${window.location.origin}${location.pathname}${location.search}`; const redirectUrl = `/login?returnUrl=${encodeURIComponent(returnUrl)}`; window.location.replace(redirectUrl); } }, [isAuthenticated, loading, location.pathname, location.search]);
if (loading) return <p>Checking authentication...</p>;
if (!isAuthenticated) return <p>Not authenticated — redirecting...</p>;
return children;};
export default ProtectedRoute;