Files
website/client/src/components/RequireAuth.jsx
2026-06-26 21:51:27 -05:00

21 lines
630 B
JavaScript

import { Navigate, useLocation } from 'react-router-dom'
import { useAuth } from '../contexts/AuthContext.jsx'
// Gate for /admin/* — redirects to the login screen when not authenticated.
export default function RequireAuth({ children }) {
const { user, loading } = useAuth()
const location = useLocation()
if (loading) {
return (
<div style={{ minHeight: '100vh', display: 'grid', placeItems: 'center', background: 'var(--bg-deep)' }}>
<span className="spin" />
</div>
)
}
if (!user) {
return <Navigate to="/admin/login" state={{ from: location }} replace />
}
return children
}