Users and sign-in
Sessions and sign-out
A completed sign-in creates a session: an access token valid for 10 minutes plus a refresh token. The access token goes to your backend with each request. The refresh token stays with the SDK, which uses it to get a new pair when the access token runs out.
getToken and refresh rotation
getToken() returns the current access token. When the token expires in less than 60 seconds, it refreshes the pair first and returns the new token. When nobody is signed in, it returns null. Call it before every request instead of keeping the token in a variable.
async function api(path: string, init: RequestInit = {}) {
const token = await fox.getToken();
if (!token) throw new Error("Not signed in");
return fetch(path, {
...init,
headers: { ...init.headers, Authorization: `Bearer ${token}` },
});
}Refresh tokens are single-use. Each refresh returns a new one and invalidates the old one. If an old refresh token is presented again, Fox Login ends the whole session for every holder, because one of them has to be a copy. For the same reason the SDK runs one refresh at a time and lets concurrent getToken() calls share it. refresh() forces a refresh.
When the refresh token is no longer valid, the server answers 401 invalid_refresh_token. The SDK then clears the local session and reports the user as signed out. Refreshes are limited to 600 per minute per IP address.
Where the session is kept
| Storage | How to use it | Trade-off |
|---|---|---|
| Memory (default) | Nothing to pass | A page reload signs the user out. A script injected into the page cannot read the refresh token from storage. |
localStorage |
storage: localStorageStorage() |
Users stay signed in across reloads. A script injected into your page could read the refresh token. |
| Your own | storage: { get, set, remove } |
For mobile, back it with the Keychain or the Android Keystore. Methods may return promises. |
import { FoxLogin, localStorageStorage } from "foxlogin";
const fox = new FoxLogin({
publishableKey: "pk_live_eu_…",
storage: localStorageStorage(),
});
await fox.ready(); // the stored session is loaded
const stop = fox.onChange((state) => {
if (state.status === "signed_in") renderAccount(state.user);
else renderSignIn();
});fox.user is the signed-in user or null, and fox.state is { status: "signed_in", user } or { status: "signed_out" }. Read them after ready() resolves. onChange fires on sign-in, refresh and sign-out, and returns a function that removes the listener.
Sign-out
signOut() clears the local session and revokes it on the server. Fox Login's own endpoints reject the session's tokens from then on.
await fox.signOut();Your backend verifies tokens locally, so it accepts an access token it already received until the token's exp, at most 10 minutes later. If you need a hard cut-off, keep a short list of revoked sid values on your side and check the sid claim against it.
confirmPasswordReset ends every other session of the user, and reuse of a refresh token ends the session in which it happened.