Hooks & guards
The full hook list, conditional-render guards, and calling your own API with `useAuthHeaderFn`.
The provider, hooks, and guards are re-exported from @ackplus/nest-auth-react and behave identically on React Native — only their web-only code paths are feature-detected away. Pick the narrowest hook for the job: components that subscribe to less re-render less.
useNestAuth()
The full context value: all state plus every action. Use it when a component needs both.
status—'loading' | 'authenticated' | 'unauthenticated'.sessionData— the session user, ornull.session— the session metadata (id, userId, tenantId, expiry).client— the underlyingAuthClient, reachable from anywhere in the tree.setMode('header' | 'cookie')/getMode()— read or change the token transport. React Native uses'header'.
Focused hooks
Reach for these in components that only need a slice of auth state — they re-render only when that slice changes.
useUser()
The session user, or null.
useSession()
The session metadata object (not the user) — id, userId, tenantId, expiresAt, createdAt.
useAccessToken()
The current access token string, or null. In header mode (the RN default) this is the raw JWT — handy for one-off integrations like websockets or third-party SDKs. For your own API calls, prefer useAuthHeaderFn below.
useAuthStatus()
Authentication status with derived booleans:
isLoading is true on first mount until the provider resolves the user.
useHasRole(role)
Returns true if the current user has the role; false when unauthenticated.
useHasPermission(permission)
Same shape, for permissions.
Guards (conditional render)
Guard components render their children only when an auth condition holds — ideal for navigators and screens.
<AuthGuard>
Renders children only when authenticated. Provide a fallback for the unauthenticated case.
<GuestGuard>
The opposite — renders children only when not authenticated. Use it to keep already-signed-in users off the login and signup screens.
<RequireRole>
Renders children only if the user has the given role.
<RequirePermission>
Same shape, for permissions.
Guards compose — nest them to require auth and a role:
See the React guards reference for the complete prop tables (
loadingFallback,onUnauthenticated,matchAll, and the HOC variants), which apply unchanged on RN.
Calling your own API
To hit your own backend with the current access token, use useAuthHeaderFn(). It returns a stable async function that reads the live token on demand and resolves to a headers object you spread onto a request:
It works the same with axios:
Because the function reads the token at call time, it always sends the freshest token — including one the client just auto-refreshed.
Related
- Authentication — the action methods in depth.
- MFA & tenancy.
- React hooks and React guards — the shared source of these APIs.