# Refresh a token (https://effetch.monarcode.dev/docs/plugins/token-refresh)

Access tokens expire, often in the middle of a session, and a call made with
an expired one gets a `401`. Rather than fail that call, `bearer` can get a
new token and send the request again. This page shows how to set that up,
and how refreshing behaves when several calls are refused at the same time.

## Add a refresh function

When a request that `bearer` sent a token with gets a `401`, `bearer` asks
`refresh` for a new token, then sends the request again with it. Pass
`refresh` next to `token`:

```ts
import { Fetch } from "@monarcode/effetch";
import { bearer } from "@monarcode/effetch/plugins";

declare const tokens: {
  readonly access: () => string | undefined;
  readonly renew: () => Promise<string | undefined>;
};

export class Api extends Fetch.Service<Api>()("Api", {
  baseUrl: "https://api.example.com",
  plugins: [
    bearer({ token: () => tokens.access(), refresh: () => tokens.renew() }),
  ],
}) {}
```

`refresh` gets the refused request, as it was sent, with its
`Authorization` header. Like `token`, it can be a function, sync or async, or
an Effect, whose error and services become the client's.

`bearer` uses the new token for that request only. Have `refresh` store it
where `token` reads it, so later requests send it too.

## Make the refresh call skip bearer

When `refresh` is a function that runs your client to get the new token, the
refresh call must skip `bearer` with `plugins: { bearer: false }`. `bearer`
can't tell that call apart from any other, so without it, a `401` to the
refresh call would wait for the refresh it's part of, and never end:

```ts
import * as Effect from "effect/Effect";
import * as FetchHttpClient from "effect/http/FetchHttpClient";
import * as Layer from "effect/Layer";
import * as ManagedRuntime from "effect/ManagedRuntime";
import * as Schema from "effect/Schema";

import { Fetch } from "@monarcode/effetch";
import { bearer } from "@monarcode/effetch/plugins";

declare const tokens: {
  readonly access: () => string | undefined;
  readonly refresh: () => string | undefined;
  readonly save: (access: string) => void;
};

const Renewed = Schema.Struct({ accessToken: Schema.String });

export class Api extends Fetch.Service<Api>()("Api", {
  baseUrl: "https://api.example.com",
  plugins: [bearer({ token: () => tokens.access(), refresh: () => renew() })],
}) {}

const runtime = ManagedRuntime.make(
  Api.layer.pipe(Layer.provide(FetchHttpClient.layer)),
);

function renew(): Promise<string | undefined> {
  return runtime.runPromise(
    Effect.gen(function* () {
      const api = yield* Api;
      const { accessToken } = yield* api.post("/auth/refresh", {
        body: { refreshToken: tokens.refresh() },
        output: Renewed,
        plugins: { bearer: false },
      });
      tokens.save(accessToken);
      return accessToken;
    }).pipe(Effect.orElseSucceed(() => undefined)),
  );
}
```

When the refresh call fails, such as when the refresh token has expired too,
`renew` gives `undefined`, so the original call ends with its `401`. A
promise that rejects would make the call die instead.

An Effect `refresh` can't hang this way: calls made inside it never
refresh. Skipping `bearer` there still keeps the expired token off the
refresh call.

## How refreshing behaves

Refreshing follows these rules when calls are refused, fail or are sent
again:

**Details: how refreshing behaves**

- **Calls refused together share one refresh.** Calls refused with the same
  token, and sent before its refresh ended, share that refresh: `refresh`
  runs once for all of them, and they all get its token, or its error. A
  call refused later with the same token starts a new refresh, which happens
  when `refresh` doesn't store the token where `token` reads it.
- **Different tokens refresh apart.** Calls refused with different tokens,
  such as other users' on a server, get their own refresh.
- **One caller can't cancel it for the others.** The shared refresh runs on
  its own. A call that times out or is interrupted while it waits stops
  waiting, but the refresh carries on for the others.
- **The request is sent again once.** If the new token is refused too,
  the call ends with that `401`, which fails as any other `401` does.
- **No token means no retry.** When `refresh` gives `null`, `undefined` or
  `""`, such as when the session has ended, the call ends with the `401`,
  and nothing more is sent.
- **Some requests aren't refreshed:** a request that came with its own
  `Authorization` header, one sent without a token, and one with a stream
  body, which can't be sent twice. Neither is a call that skips `bearer`.
- **Every method is sent again,** a `POST` included: a `401` means the
  server refused the request before acting on it.
- **A throw is a defect.** A `refresh` function that throws, or whose
  promise rejects, makes the call die. To fail with a typed error, pass an
  Effect.

## What's next

- [Plugin recipes](https://effetch.monarcode.dev/docs/plugins/recipes): sign out and fail with a typed
  error on a `401`, in place of refreshing.
- [Change plugins for one call](https://effetch.monarcode.dev/docs/plugins/per-call): skip `bearer` on
  other calls, such as a login.
- [Built-in plugins](https://effetch.monarcode.dev/docs/plugins#how-plugins-nest): see how a refreshed
  request shares its attempt's timeout.