# effetch (https://session-kit.pages.dev/docs/reference/effetch)

Import these from `@monarcode/session-kit/effetch`. To set it up, see
[Effect and effetch](https://session-kit.pages.dev/docs/effect).

## Functions

### sessionBearer

An effetch plugin that sends the current access token as `Authorization: Bearer`. After a 401, it gets a new access token once. `GET` and `HEAD` requests are then sent again. Other requests keep their 401, which effetch fails the call with as an `HttpError`, for you to retry. If the request sent again gets a 401 too, the user is signed out, unless `signOutOnRepeated401` is off. If the renewal ends the session, the call fails with the 401 too.

Calls fail with `AuthFailure`, before sending anything: `UNAUTHENTICATED` when signed out, unless `optional` sends them without a token, and `LOCKED` while locked. When a refresh fails, the call fails with that failure, such as `REFRESH_FAILED`.

If the session changes during a call, it fails with `SESSION_CHANGED` rather than return a response meant for another account.

Streams, such as `api.sse` and stream endpoints, work the same way up to the response's headers. A stream that's open when the user signs out or switches accounts keeps reading until it ends. Stop it yourself if it mustn't outlive the session.

The token never leaves your API. A request on another origin, or with a username or password in its URL, dies with `INVALID_OPTIONS`. With Effect's `FetchHttpClient`, a redirect fails as a `Fetch.NetworkError` rather than being followed, except on React Native before Expo SDK 58, whose `fetch` follows redirects whatever it's told. An invalid `baseUrl` throws `INVALID_OPTIONS`.

A request that already has an `Authorization` header is sent as it is, as effetch's `bearer` does.

Put it last in `plugins`, after `retry`, so each attempt reads the current token. The client then needs the `Auth` service, but calls don't.

On the server, make the effetch client per request, and provide it the `Auth` layer for that request's session (`RequestAuth`). It reads the token from the request's cookie and saves a renewed one.

See [`SessionBearerOptions`](#sessionbeareroptions) for `optional` and `signOutOnRepeated401`.

## Types

### SessionBearerOptions

Options for [`sessionBearer`](#sessionbearer).

### SessionBearerOptions

Options for sessionBearer.

| Prop                    | Type     | Description                                                                                                                                                                                                                                                                    |
| ----------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `baseUrl`               | `string` | Your API's URL, usually the effetch client's `baseUrl`. It must be absolute, such as `https://api.example.com`. The access token goes only to requests on its origin.                                                                                                          |
| `optional?`             | `union`  | Whether a call made while signed out is sent without an `Authorization` header, rather than failing with `UNAUTHENTICATED`. Set `true` when one client serves public and private endpoints. A locked client still fails with `LOCKED`. Calls can override it. Default: `false` |
| `signOutOnRepeated401?` | `union`  | Whether a second 401, after a successful refresh, signs the user out. Set `false` when some endpoints answer 401 for reasons other than a rejected token. Calls can override it. Default: `true`                                                                               |

### SessionBearerPlugin

The plugin [`sessionBearer`](#sessionbearer) returns. Its calls can fail with `AuthFailure`, and a client made with it needs the `Auth` service.

A call can skip it with `plugins: { sessionBearer: false }`, or override `optional` and `signOutOnRepeated401`.