Using the client
The client's methods at a glance, for loaders, event handlers and request code, and the options that change how it holds the user.
Your components read the session with hooks, but some of your code runs
outside React: a route loader, a click handler, the code that calls your API.
That code talks to the client that createAuth returned, which these docs
call auth.
This section covers the client itself: the methods that code outside React
calls, and two createAuth options that change how the client holds the
user, reading it from the access token and locking. The examples build on the
app from Getting started, where auth is the client
and authFetch its fetch.
The client's methods
Here's what each method does:
| Call | What it does |
|---|---|
auth.state.get(), .subscribe() | Reads the state, or follows its changes. |
auth.getSession() | Resolves who is signed in, or null. It reads the saved session first if needed, and refreshes an access token that expired. |
auth.signIn({ ...tokens, user }) | Saves a new session, replacing any current one. |
auth.signOut() | Ends the session and clears the saved one. Other tabs sign out too, when the storage reports changes and locking is off. |
auth.updateUser(user) | Replaces the user and keeps the session. A client that reads the user from the access token doesn't have it. |
auth.refresh() | Refreshes now, even while the access token still works. |
auth.retry() | Tries again after a refresh failed. |
auth.isCurrent(session) | Checks whether a session is still the signed-in one. |
auth.credentials.get(), .renew(), .reject() | Resolves the access token, and replaces it or ends the session after a 401, for code that sends requests. |
auth.lock(), auth.unlock() | Locks and unlocks a saved session, with the locking option. |
auth.mount() | Starts the expiry timers and following other tabs. AuthProvider, TanStack Router's connectAuth, React Router's SessionOutlet and Expo's SessionGate call it for you. |
Each method's JSDoc says what it resolves and which AuthError it rejects
with, so your editor shows the details where you call it. The
AuthClient reference has the same
text.
With server rendering
When the session lives on the server, the browser's client comes from
createMirrorAuth instead. It reads the state and the session the same way,
but it has no lock, unlock or updateUser, and its signIn and
credentials need options of their own.
Server rendering covers what it can do.
In this section
Each page covers one job:
- StateRead the state outside React, scope cached data to the session, and know what each field holds.
- Sessions and credentialsAsk who is signed in, and keep the access token apart from code that only needs the user.
- Signing in and outSave a new session, end it in every tab, and revoke its tokens.
- Updating the userReplace the user without ending the session, and handle each way it can fail.
- Authenticated requestsA fetch that sends the access token to one API, and refreshes after a 401.
- Other HTTP clientsSend the access token with ky, axios, openapi-fetch or any other client.
- Users from the access tokenRead the user from a JWT's claims instead of the sign-in response.
- LockingAsk a returning user to unlock, for example with biometrics, in native apps.
Next steps
- Sessions and credentials: ask who is signed in from a loader or an event handler.
- Authenticated requests: call your API with the access token.
- Configuring the client: change how the client saves, refreshes and reports errors.