29 lines
668 B
TypeScript
29 lines
668 B
TypeScript
// 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}</>;
|
|
} |