18 lines
488 B
TypeScript
18 lines
488 B
TypeScript
import { Navigate, useLocation } from 'react-router-dom';
|
|
import { useAuth } from './AuthContext';
|
|
|
|
interface ProtectedRouteProps {
|
|
children: React.ReactNode;
|
|
}
|
|
|
|
export function ProtectedRoute({ children }: ProtectedRouteProps) {
|
|
const { isAuthenticated } = useAuth();
|
|
const location = useLocation();
|
|
|
|
if (!isAuthenticated) {
|
|
// Redirect to login with the current location saved
|
|
return <Navigate to="/login" state={{ from: location }} replace />;
|
|
}
|
|
|
|
return children;
|
|
} |