# Using the client (https://session-kit.pages.dev/docs/client)

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](https://session-kit.pages.dev/docs/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](https://session-kit.pages.dev/docs/client/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`](https://session-kit.pages.dev/docs/client/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](https://session-kit.pages.dev/docs/reference/core#authclient) 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](https://session-kit.pages.dev/docs/server-rendering) covers what it can do.

## In this section

Each page covers one job:

- [State](https://session-kit.pages.dev/docs/client/state): Read the state outside React, scope cached data to the session, and know what each field holds.

- [Sessions and credentials](https://session-kit.pages.dev/docs/client/sessions): Ask who is signed in, and keep the access token apart from code that only needs the user.

- [Signing in and out](https://session-kit.pages.dev/docs/client/signing-in-and-out): Save a new session, end it in every tab, and revoke its tokens.

- [Updating the user](https://session-kit.pages.dev/docs/client/updating-the-user): Replace the user without ending the session, and handle each way it can fail.

- [Authenticated requests](https://session-kit.pages.dev/docs/client/requests): A fetch that sends the access token to one API, and refreshes after a 401.

- [Other HTTP clients](https://session-kit.pages.dev/docs/client/other-http-clients): Send the access token with ky, axios, openapi-fetch or any other client.

- [Users from the access token](https://session-kit.pages.dev/docs/client/users-from-tokens): Read the user from a JWT's claims instead of the sign-in response.

- [Locking](https://session-kit.pages.dev/docs/client/locking): Ask a returning user to unlock, for example with biometrics, in native apps.

## Next steps

- [Sessions and credentials](https://session-kit.pages.dev/docs/client/sessions): ask who is signed in
  from a loader or an event handler.
- [Authenticated requests](https://session-kit.pages.dev/docs/client/requests): call your API with the
  access token.
- [Configuring the client](https://session-kit.pages.dev/docs/configuration): change how the client saves,
  refreshes and reports errors.