AuthKitProvider is a client-side React component that wraps your application and provides authentication context to all child components. It manages user state, session data, and provides methods for authentication operations.
Usage
Props
The child components that will have access to the authentication context.
Customize what happens when a session expires. By default, the entire page will be reloaded. Pass
false to disable expired session checks.Initial auth data from the server. If provided, the provider will skip the initial client-side fetch, improving performance and reducing loading states.
Context value
The provider exposes the following values through theuseAuth hook:
The authenticated user object, or
null if not authenticated.The current session ID.
The ID of the organization the user is currently in.
The user’s role in the current organization.
Array of all roles assigned to the user.
Array of permissions granted to the user.
Array of entitlements for the user.
Array of feature flags enabled for the user.
Information about the admin impersonating this user, if applicable.
Indicates whether authentication data is being loaded.
Fetches the latest authentication state from the server.
refreshAuth
(options?: { ensureSignedIn?: boolean; organizationId?: string }) => Promise<void | { error: string }>
Refreshes the authentication session, optionally switching to a different organization.
Signs out the current user and optionally redirects to a specific URL.
switchToOrganization
(organizationId: string, options?: SwitchToOrganizationOptions) => Promise<Omit<UserInfo, 'accessToken'> | { error: string }>
Switches the user’s active organization.
Session expiration handling
The provider automatically monitors session validity when the browser tab becomes visible or gains focus. If a session has expired, it will:- Call the
onSessionExpiredcallback if provided - Otherwise, reload the page (default behavior)
- Do nothing if
onSessionExpired={false}
Best practices
Use initialAuth for better performance
Pass server-side authentication data to
initialAuth to avoid an extra client-side fetch on page load.Disable session checks cautiously
Only set
onSessionExpired={false} if you have custom session management. The default behavior protects against stale sessions.Related
useAuth
Access the authentication context
useAccessToken
Get the user’s access token