Expiry
How the client finds out when the access token expires, and which of your backend's answers to prefer.
The client refreshes the access token before it expires, so it reads when that is from your tokens. Your backend can say so in a few ways: a lifetime in the sign-in response, a fixed time, or claims inside a JWT.
This page shows where the client reads the expiry from, which field to send, and what happens when there's none.
Send the token's lifetime
Most backends answer with a lifetime, such as OAuth's expires_in. Pass it
as expiresIn, in seconds:
import type { } from "@monarcode/session-kit";
type = {
: string;
: string;
: number;
};
export const = (: ): => ({
: .,
: .,
: .,
});The client counts expiresIn from when it receives the tokens, so a wrong
device clock doesn't matter. Send expiresAt, in Unix milliseconds, only
when your backend gives no lifetime.
Pass expiresIn or expiresAt, not both. Tokens with both are an
INVALID_SESSION error. See Tokens for
each field.
Where the expiry comes from
When your tokens hold more than one of these, the client uses the first it finds:
expiresInorexpiresAt.- A JWT access token's
expandiatclaims, as a lifetime, whenexpis later thaniat. - A JWT access token's
expclaim alone, as a time.
A JWT with iat works like expiresIn: the client counts the lifetime from
when it receives the token. So if your access token is a JWT with both
claims, you don't need to send an expiry at all.
Fixed times and device clocks
expiresAt and a JWT's exp alone are fixed times, which the client
compares with the device's clock. Device clocks can be minutes off. A clock
that runs fast makes the token look already expired, and signIn rejects
it with INVALID_SESSION.
Tokens without an expiry
When the client can't tell when the token expires, it uses the token until
your API rejects it. authFetch then refreshes after
the 401. There's no early refresh, since there's nothing to count down to.
Next steps
- Refresh: see when the client refreshes, and what it does when a refresh fails.
- Authenticated requests: send the access token with each request, and refresh after a 401.
Tokensreference: every fieldsignInand your refresh callback take.