ailita_rt cookie. When the refresh token is exhausted (expired, revoked, or invalidated by the server), the library clears all auth state and fires onSessionExpired. Your responsibility is wiring this callback to redirect the user to the login page, and optionally giving users visibility into their active sessions via the useSessions hook.
onSessionExpired
onSessionExpired is a prop on AilitaProvider. Although it is typed as optional (() => void | undefined), it is a required integration step in any production application. If omitted, sessions expire silently: tokens are cleared but the user receives no redirect and no error — they are left in a broken state with stale UI and failing API calls and no recovery path.
What triggers onSessionExpired
A request returns HTTP 401
An authenticated API call receives a 401 response. The library detects the expired access token.
Concurrent requests are queued
The failing request and all subsequent requests made while the refresh is in progress are queued. No requests are dropped — they will replay if the refresh succeeds.
One refresh attempt is made
The library attempts a single token refresh using the
ailita_rt cookie. If the refresh succeeds, all queued requests replay automatically and the user experiences no interruption.Refresh fails — cleanup runs
If the refresh fails (cookie expired, revoked, or server rejection): all queued requests are rejected,
clearTokens() fires (the access token is cleared from memory and the ailita_rt cookie is removed), then the user state is set to null and the roles array is cleared.Wiring onSessionExpired
The
reason=expired query parameter is a UX convention — show a “Your session has expired, please log in again” message on the login page when this parameter is present. This tells the user why they were redirected rather than leaving them confused about why they ended up on the login page.What happens if onSessionExpired is omitted
useSessions
useSessions fetches the authenticated user’s active sessions from GET /users/me/sessions on mount and exposes methods to revoke individual sessions or sign out all devices. Use it to build a “Manage devices” UI in profile settings, giving users visibility into where they are logged in and the ability to terminate sessions they do not recognize.
Return Shape
The list of active sessions. The entry with
current: true is the user’s current device and session.True during the initial session fetch and during revoke operations. Use this to show a loading indicator while sessions are being loaded or a revoke is in progress.
Last error message from a fetch or revoke operation. Cleared automatically at the start of the next operation. Render this to surface API errors to the user.
Re-fetches
GET /users/me/sessions. Use after external revocations or when you want to force a sync with the server.Revokes a single session by ID. The session is removed from the local
sessions array immediately on success. Throws on API error — wrap in try/catch if you need to handle errors inline.Revokes all sessions. Clears the local
sessions array on success. Throws on API error. Note: the backend may keep the current session active depending on your configuration — see the note below.SessionResponse Type
Building a Sessions List
revokeAll signs out all OTHER devices — the current session remains active. If you want to sign the user out of all devices including the current one, call revokeAll() followed by the logout() method from useAuth().Next steps
Hooks Reference
Complete reference for all hooks — useAuth, useUser, useSessions, useMerchant, and useRoles — with return shapes and code examples in one place.
Auth Flows
Understand the full login state machine and challenge lifecycle.

