Skip to Content
Plugins

Refresh a token

Renew an expired access token with the bearer plugin and send the request again.

To renew an expired access token without failing the call, give bearer a refresh. This page shows how to set it up and how refreshing behaves.

Add a refresh function

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

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

declare const : {
  readonly : () => string | undefined;
  readonly : () => <string | undefined>;
};

export class  extends .<>()("Api", {
  : "https://api.example.com",
  : [
    ({ : () => .(), : () => .() }),
  ],
}) {}

The new token is used for that request only. Store it where token reads it, so later requests send it too. To sign out instead, see the Sign out on a 401 recipe.

How refreshing behaves

What's next

On this page