<AilitaProvider> and <AuthProvider> ancestors. These are already
provided if you used the standard <AilitaProvider> setup — it wraps children with both
providers automatically. useAuth provides authentication state and actions. useUser
provides profile management for the signed-in user.
useAuth
useAuth is the primary hook for authentication state and actions. It returns the current
user, authentication status, and methods to login, logout, and signup.
Return Shape
The authenticated user object.
null while loading or when not authenticated.true when the user is logged in with valid tokens.true during silent token refresh on mount and during login/logout operations.On app mount,
isLoading is true for 200–500 ms while the library attempts silent
refresh. This is why AuthGuard requires a loadingFallback — without it, the
flash-redirect anti-pattern occurs (the user sees a redirect to /login before the
refresh completes).Initiates login. Returns the full
LoginResponse for branch detection (e.g., checking
whether a challenge is required). Tokens are set automatically on a successful password
login. extraData passes additional fields (such as loginMode) to the API.Calls the server logout endpoint, then clears local tokens and auth state. Logout errors
are swallowed — local state is always cleaned up.
Initiates signup. The
app and mid fields are injected from the merchant context
automatically — you do not need to pass them.Last error message from a login, signup, or logout operation. Cleared automatically
at the start of the next attempt.
Manually clear the
error state (useful when navigating away from a form).Common Patterns
user may be null briefly during the initial silent refresh, even inside an
AuthGuard-protected route. Always use optional chaining (user?.firstName) or check
isAuthenticated first.Anti-pattern: userId from onSuccess
A common mistake is using theuserId returned by onSuccess to fetch user data
independently. This creates a redundant network request and a separate copy of user state
that can drift from what the library manages.
useUser
useUser provides methods for the authenticated user to manage their own profile, password,
email, and TOTP settings. It shares the same user object as useAuth — both read from
AuthContext, so updates are reflected everywhere immediately.
Return Shape
The same reference as
useAuth().user. Reflects the latest profile state.Loading state for profile operations (not auth loading).
true while an update,
password change, email change, or TOTP operation is in progress.Last error message from a profile operation. Cleared automatically on the next attempt.
Re-fetches
GET /users/me and updates the shared user state. Use this after external
changes or to force a sync with the server.Updates
firstName, lastName, and/or phone. Returns the updated user.The shared user state is updated automatically — all components reading
useAuth().user
or useUser().user will reflect the change without a page refresh or manual refetch.Changes the user’s password. Requires
currentPassword and newPassword. If the user
has TOTP enabled, totpCode is also required.Initiates an email address change. Returns a
challengeId that you pass to ChallengeView
to complete verification via the OTP sent to the new address.Starts TOTP setup. Returns a
qrUri to render as a QR code and backupCodes to display
to the user before they scan.Confirms TOTP setup with a 6-digit code from the authenticator app. Automatically calls
refreshProfile() — user.totpEnabled will be true after this resolves.Disables TOTP with a 6-digit verification code. Automatically calls
refreshProfile() —
user.totpEnabled will be false after this resolves.Manually clear the
error state.Profile Update Example
Password Change Example
Key Types
Next steps
Signup & Forgot Password
Walk through SignupForm and ForgotPasswordForm integration, including post-signup
redirect and OTP verification flows.
Auth Flows
Understand the full login, challenge, and token refresh lifecycle — useful if you
arrived at this page before reading the flow overview.

