Skip to Content
Using the client

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:

CallWhat 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:

Next steps

On this page