Files
CityCards-Website/src/components/ProtectedRoute.tsx

29 lines
668 B
TypeScript
Raw Normal View History

2026-04-24 19:59:02 +05:30
// ProtectedRoute.tsx
import { useState } from 'react';
import { useAuth } from '../context/AuthContext';
import { LoginModal } from './LoginModal';
import { useNavigate } from 'react-router-dom';
interface ProtectedRouteProps {
children: React.ReactNode;
}
export function ProtectedRoute({ children }: ProtectedRouteProps) {
const { user } = useAuth();
const navigate = useNavigate();
const [isLoginOpen, setIsLoginOpen] = useState(!user);
if (!user) {
return (
<LoginModal
isOpen={isLoginOpen}
onClose={() => {
setIsLoginOpen(false);
navigate(-1);
}}
/>
);
}
return <>{children}</>;
}