File size: 637 Bytes
7f6dd09 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import { Navigate, useLocation } from "react-router-dom";
import { useAuth } from "../context/AuthContext.jsx";
export default function ProtectedRoute({ children }) {
const { user, loading } = useAuth();
const location = useLocation();
if (loading) {
return (
<div className="page flex items-center justify-center">
<div className="spinner" style={{ borderColor: 'var(--border)', borderTopColor: 'var(--accent)' }}></div>
</div>
);
}
if (!user) {
// Redirect unauthenticated users to login page
return <Navigate to="/auth" state={{ from: location }} replace />;
}
return children;
}
|