import { Navigate, useLocation } from "react-router-dom"; import { useAuth } from "../context/AuthContext"; import type { ReactNode } from "react"; /** * Wraps routes that require authentication. * Redirects to /login with a returnTo param if not authenticated. * Shows nothing while the auth state is still loading (token rehydration). */ export default function ProtectedRoute({ children }: { children: ReactNode }) { const { isAuthenticated, loading } = useAuth(); const location = useLocation(); if (loading) { return null; // Wait for token rehydration before redirecting } if (!isAuthenticated) { const returnTo = location.pathname + location.search; return ; } return <>{children}; }