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
refreshis a function of the refused request, as it was sent, sync or async, or an Effect. Liketoken, an Effect's error and services become the client's.- Calls refused with the same token share one refresh, so the token is renewed once, and they all get its token or its error. Calls refused with different tokens, such as other users' on a server, get their own.
- A call that times out or is interrupted while it waits doesn't cancel the refresh for the others.
- The request is sent again once. If the new token is refused too, the call
ends with that
401. - When
refreshgivesnull,undefinedor"", such as when the session has ended, the call ends with the401, and nothing more is sent. - A request with its own
Authorizationheader, sent without a token, or with a stream body, which can't be sent twice, isn't refreshed. Neither is a call that skipsbearer. - Make the refresh call itself skip
bearer:plugins: { bearer: false }. Calls made inside an Effectrefreshnever refresh, but a function that runs the client withEffect.runPromisecan't be told apart: a401to it would wait for the refresh it's part of. - Every method is sent again, a
POSTincluded: a401means the server didn't act on the request.
What's next
- Plugin recipes: sign out and fail with a typed
error on a
401, in place of refreshing. - Change plugins for one call: make the refresh
call skip
bearer. - Plugin order: see how a refreshed request shares its attempt's timeout.