chrysopedia/frontend/src/components/ProtectedRoute.tsx
jlightner c60fc8c3b3 feat: Added ProtectedRoute component, CreatorDashboard with sidebar nav…
- "frontend/src/components/ProtectedRoute.tsx"
- "frontend/src/pages/CreatorDashboard.tsx"
- "frontend/src/pages/CreatorDashboard.module.css"
- "frontend/src/pages/CreatorSettings.tsx"
- "frontend/src/pages/CreatorSettings.module.css"
- "frontend/src/App.tsx"
- "frontend/src/App.css"
- "frontend/src/pages/Login.tsx"

GSD-Task: S02/T04
2026-04-03 22:02:04 +00:00

24 lines
803 B
TypeScript

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 <Navigate to={`/login?returnTo=${encodeURIComponent(returnTo)}`} replace />;
}
return <>{children}</>;
}