JavaScript SDK

Sessions

Read the session and access token, refresh it, sync it across tabs, sign out, and list or revoke the user's sessions.

A session is an access token and, when the app hands one out, a refresh token. Both are JWTs. The client stores them (see Client setup) and refreshes the access token before it expires.

Read the session#

const { data, error } = await scute.getSession();// data.session: Session// data.user: ScuteUserData | null

getSession() refreshes an access token that expires within 10 seconds, then fetches the user from the API.

type Session =  | { status: "authenticated"; access: string; accessExpiresAt: Date; refresh?: string; refreshExpiresAt?: Date }  | { status: "unauthenticated" | "loading"; access: null; accessExpiresAt: null };

sessionUnAuthenticatedState() and sessionLoadingState() return the two empty states.

Get the access token#

const { data, error } = await scute.getAuthToken();// data.access, or error: InvalidAuthTokenError when there is no valid token

The token is refreshed first when it expires within 10 seconds, so the result is never an expired token. Send it to your own API as Authorization: Bearer <token>. Scute's own endpoints read it from X-Authorization; accessTokenHeader(token) and refreshTokenHeaders(token) build those headers.

Refresh#

MethodDoes
refreshSession()Refreshes now with the stored refresh token. Returns { data: session }.
refetchSession()Re-reads the user from the API. Fires session_refetch.
startAutoRefresh() / stopAutoRefresh()Start or stop the background refresh. It runs by default in a browser.
setRefreshProxyCallback(fn)Refresh through your backend instead of with a refresh token in the browser. fn returns a token payload ({ access, access_expires_at }). @scute/nextjs-handlers uses this.

A refresh that fails with 401 or 403 ends the session: session_expired fires and the stored tokens are removed. A network error, a 5xx, or a 403 for a blocked network (ip_blocked, country_blocked) keeps the stored session for the next try. Refresh requests are never retried on their own, so one refresh token is never sent twice.

When the app's idle timeout ended the session, scute.sessionEndReason is "session_idle" until the next sign-in.

Across tabs#

In a browser with persistSession on, the client posts each auth event on a BroadcastChannel named sct_broadcast__<appId>. Other tabs take only the event name from the message and read the session again from storage and the API. Signing in or out in one tab shows up in the others.

Sign out#

const revoked = await scute.signOut();

Signs out locally first (the stored tokens are removed and signed_out fires), then revokes the session on the server. Returns false when the server revocation failed.

The user's sessions#

const { data: sessions } = await scute.listUserSessions();await scute.revokeSession(sessions[0].id);

Each session (ScuteUserSession):

FieldType
idstring | number
type"webauthn", "magic", "xlogin", "oauth", "misc", "otp", "workspace", "m2m", "mfa", "challenge" or "impersonation"
display_name, nicknamestring
platform, browser, user_agent, user_agent_shortnamestring
last_used_at, last_used_at_ipstring
credential_idThe passkey behind the session, or null
created_at, updated_atstring

revokeSession(id, credentialId?) ends that session at once. When the session was made with a passkey, the credential is also forgotten on this device. Server side, with the secret key, see Admin API.