HR-ATS-Portal/frontend/src/auth/RequireAuth.jsx

41 lines
1.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import { Navigate, useLocation } from 'react-router-dom'
import { useAuth } from './AuthContext'
import Spinner from '../components/Spinner'
/**
* Route guard.
*
* Blocking on `loading` is deliberate: rendering the shell before /users/me
* resolves would paint the full 23-item nav and then remove items a moment
* later, which reads as a bug rather than as security.
*/
export default function RequireAuth({ children, permission }) {
const { status, can } = useAuth()
const location = useLocation()
if (status === 'anonymous') {
return <Navigate to="/auth/login" replace state={{ from: location }} />
}
if (status === 'error') {
return <Navigate to="/auth/login?expired=1" replace />
}
if (status === 'loading') {
return (
<div className="route-loading">
<Spinner label="Loading your workspace" />
</div>
)
}
if (permission && !can(permission)) return <Forbidden />
return children
}
export function Forbidden() {
return (
<div className="empty-state">
<h3>You dont have access to this page</h3>
<p>Ask an administrator to grant your role the required permission.</p>
</div>
)
}