Skip to main content
@glinr/theauth-client and @glinr/theauth-react handle the client side of persistent sessions. Against a server without the feature, nothing here sends a refresh request, because the server never answers session_expired.

Session store

After a refresh the new session lives in the cookie, so the store drops its stored bearer token. Reconnect streams of your own, such as WebSockets or event sources, once a refresh succeeds.

RefreshSessionResult

sessionEnded is true only when the server said the device is over (remember_invalid, remember_expired, remember_reused or refresh_denied). status is 0 when no answer arrived. refreshSession() never throws, and the store signs out only if the server also reports no session.

How the retry behaves

fetch clones the request before the first attempt and retries at most once.
  • A 401 without the code session_expired is returned untouched.
  • A request with a ReadableStream body is not retried, because the body cannot be sent twice.
  • If the refresh gets no answer, fetch throws a TypeError, like any network failure.
  • If the refresh answers 429 or 5xx, you get that status in place of the 401, so an outage never looks like a sign-out.
  • If the refresh says the device is over, you get the original 401.

Without the store

refresher.refresh() returns the same RefreshSessionResult. SESSION_EXPIRED_CODE is the string to match if you write the retry yourself.

Tabs

Only one refresh runs at a time in a tab, and navigator.locks serializes tabs. Where navigator.locks is missing, outside secure contexts, a best effort storage lease stands in. The server tolerates parallel refreshes inside the grace window either way. Tabs tell each other about sign in, sign out and rotation through BroadcastChannel, or storage events without it. Messages carry no token and no user data. The storage lease and the storage event fallback need a storage option on the store.

React

While status is loading, user and session are null, so a cached sign in does not flash before the server confirms it. usePersistentSession() returns status (loading, authenticated or unauthenticated), session, user, isAuthenticated, isLoading, signIn, isSigningIn, error, signOut, refreshSession and fetch. Use that fetch for your own API calls so they refresh and retry like the hooks do.

External auth

With external auth (<TheAuthProvider external={...}>), signIn(email, password, { remember }) adds remember=1 or remember=0 to the login redirect. Set refreshPath to your backend’s refresh route, /auth/session/refresh under the apiUrl mount of a theAuth or theauth-go server. When mePath then answers session_expired, the provider refreshes once and reads it again. A network error or 5xx keeps the user as it was. refreshSession and authFetch on the context, and refreshSession and fetch from usePersistentSession(), refresh only when refreshPath is set. Without it refreshSession reports that it is not available and fetch is the plain one. Tabs refresh independently in this mode, which the server’s grace window covers.

Go server client

createTheAuthGoClient talks to a theauth-go server. It needs a server version that supports persistent sessions.
A cookie request that answers session_expired refreshes once and retries once. Requests that use getToken send a bearer token and never refresh. The client has no method that only refreshes. To do that by hand, create a refresher for the Go routes:
The TanStack Query hooks in @glinr/theauth-react/query use the same client, and useLogin() and usePasskeys().login take remember:

Server rendering

Resolve the session on the server with refreshFromRequest, forward its Set-Cookie headers, and pass the result as initialSession so the client does not start in loading. See React.

Native and other non-browser clients

Keep the remember token in the platform’s secure storage and call persistentSessions.refresh from your own endpoint, or send both cookies to the refresh route with an Origin header that matches baseUrl or a trusted origin. Run one refresh at a time and save the new tokens before using them.
Last modified on December 3, 2026