|
|
import React from 'react'; |
|
|
import { Navigate, useLocation } from 'react-router-dom'; |
|
|
import { useAuth } from '../context/AuthContext'; |
|
|
import { Loader2 } from 'lucide-react'; |
|
|
|
|
|
interface ProtectedRouteProps { |
|
|
children: React.ReactNode; |
|
|
} |
|
|
|
|
|
const ProtectedRoute: React.FC<ProtectedRouteProps> = ({ children }) => { |
|
|
const { isAuthenticated, loading } = useAuth(); |
|
|
const location = useLocation(); |
|
|
|
|
|
if (loading) { |
|
|
return ( |
|
|
<div className="flex items-center justify-center min-h-screen"> |
|
|
<Loader2 className="h-8 w-8 animate-spin text-edu-primary" /> |
|
|
</div> |
|
|
); |
|
|
} |
|
|
|
|
|
if (!isAuthenticated) { |
|
|
|
|
|
return <Navigate to="/login" state={{ from: location }} replace />; |
|
|
} |
|
|
|
|
|
return <>{children}</>; |
|
|
}; |
|
|
|
|
|
export default ProtectedRoute; |
|
|
|