Using the client
The client's methods at a glance, for loaders, event handlers, request code, and anything else outside React.
This section is for code outside React components. Components read the
session with hooks. Everything else, such as loaders, event handlers, and
request code, calls the client that createAuth returned. These are its
methods:
| Call | What it does |
|---|---|
auth.state.get(), .subscribe() | Reads the state, or follows its changes. |
auth.getSession() | Resolves who is signed in, restoring or refreshing first, or null. |
auth.signIn(tokens) | Saves a new session, replacing any current one. |
auth.signOut() | Ends the session, clears storage, and tells other tabs. |
auth.updateUser(user) | Replaces the user and keeps the session. |
auth.refresh() | Refreshes now, even while the access token is usable. |
auth.retry() | Tries again after a failed refresh. |
auth.isCurrent(session) | Tells whether a session is still the signed-in one. |
auth.credentials.get() | Resolves the access token, for code that sends requests. |
auth.lock(), auth.unlock() | Locks and unlocks a saved session, with locking. |
auth.mount() | Starts timers and tab sync. AuthProvider and the routers call it. |
Each method's JSDoc says what it resolves and which AuthError it rejects
with, so your editor shows the details where you use them.
The examples in this section build on the app from
Getting started: auth is its client, and
authFetch its fetch.
- StateRead the state outside React, and what each field holds.
- Sessions and credentialsAsk who is signed in, and keep the access token apart from the user.
- Signing in and outSave a new session, end it, and revoke its tokens.
- Updating the userReplace the user without ending the session.
- Authenticated requestsA fetch that sends the access token to one API, and refreshes on a 401.
- Other HTTP clientsAttach the access token with axios, ky, 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.