Guards
Components and HOCs that protect routes and UI.
The React layer ships four guard components and two HOCs. They're thin — under the hood they all call useAuthStatus, useHasRole, or useHasPermission and decide what to render.
<AuthGuard>
Renders children only when the user is authenticated.
| Prop | Default |
|---|---|
loadingFallback | null (renders nothing while auth is initializing) |
fallback | null (renders nothing when unauthenticated) |
onUnauthenticated | (none) |
<GuestGuard>
The opposite of <AuthGuard>. Renders children only when the user is not authenticated. Use on /login, /signup to redirect already-logged-in users away.
<RequireRole>
Render children only if the user has the required role(s).
| Prop | Type | Notes |
|---|---|---|
role | string | string[] | Required role(s) |
matchAll | boolean | Require ALL (default: ANY) |
loadingFallback | ReactNode? | While auth state loads |
fallback | ReactNode? | When the user lacks the role(s) |
onAccessDenied | () => void? | Called when fallback renders |
<RequirePermission>
Same shape as <RequireRole> but for permissions.
HOCs
For class components or higher-order composition:
withRequireRole(Component, options)
The wrapped component receives extra props: { hasRole, isLoading, isAuthenticated }.
withRequirePermission(Component, options)
Same idea, for permissions.
Factory builders
If you reuse the same role/permission policy in many places, build a partially-applied HOC:
Same pattern for createRequirePermissionHOC.
Combining guards
Nest them — they compose cleanly:
For very common combos (auth + role), wrap in a custom component to keep your route file tidy.
Related
- Hooks.
- RBAC.
- Next.js dashboard recipe — App-Router-style guards.