Skip to Content

Introduction

Keep a React app signed in with the tokens your backend issues, through reloads, expired tokens, other tabs and outages.

Your backend already signs users in: it checks a password and answers with an access token and a refresh token. Session kit takes those tokens and keeps the app signed in from there. It saves the session, restores it on reload, refreshes tokens before they expire, keeps every tab in step, and guards your routes. It works with plain React, TanStack Router and Start, React Router, Next.js, Expo Router on iOS and Android, and Effect.

This page shows what Session kit takes care of, what it leaves to you, and the few terms the rest of the docs use. To start using it, see Installation.

Why Session kit

Signing in is the easy part. Then the user reloads the page. The access token expires halfway through a form. They open a second tab, sign out of one, and switch accounts in the other. The backend goes down for a minute, just as a refresh was due.

Every app with its own backend has to get these moments right. The code for them usually ends up spread across an auth context, a fetch wrapper and the route guards, and one or two cases stay missing until a user finds them. Session kit is that code, written once and tested with each router and framework it supports.

What it does

Here's what Session kit does in each of those moments:

When…Session kit…
The page reloadsRestores the saved session and checks the user against your schema again. With synchronous storage, such as localStorage, and a synchronous schema, that happens before the first paint.
The access token is about to expireRefreshes it early, in the background, so requests don't wait.
A request gets a 401 anywayRefreshes once, however many requests got the 401, then sends the GET and HEAD requests again.
Two tabs need a refresh at the same momentLets one tab use the refresh token while the other waits and reuses the tokens it saved, in browsers with Web Locks.
The user signs out or switches account in one tabFollows in every other tab, and remounts the pages under SessionOutlet (or SessionGate on Expo), so nothing from the previous account stays on screen.
The backend rejects the refresh tokenSigns out in every tab and clears the saved session.
The backend is down when a refresh is dueKeeps the session and reports the error. While the access token still works, the user stays signed in. After it expires, the session waits for a retry. An outage is never a sign-out.
A signed-out user opens a private pageSends them to your sign-in page, and back to where they were going after they sign in.
The page renders on the serverReads the session from an encrypted cookie, so the first response is already signed in.

What it doesn't do

Session kit never signs users in. It has no sign-in form, no OAuth flow and no user database: it starts where your backend's sign-in response ends. If you need those too, use an auth service or framework such as Auth.js, Better Auth or Clerk, which manage their own sessions.

How it works

Session kit sits between your backend and your components. Tokens move between Session kit and your backend, and only the session's state reaches your components:

Your backend issues the tokens. Session kit saves them, refreshes them on time, and adds the access token to your API requests. Your components and route guards read who is signed in, and never handle a token.

Five terms come up on every page:

  • Tokens are what your backend issues. The access token goes with each request and lasts a short time. The refresh token, if there is one, is exchanged for a new access token when the old one expires.
  • The client, from createAuth, holds the tokens and does the work: saving, restoring, refreshing and following other tabs. An app usually has one, named auth in these docs.
  • A session is who is signed in: { sessionId, user }. It holds no tokens, so it's safe to render and to pass around.
  • State is what components read: status, user, sessionId and error. It updates as the session changes.
  • Credentials are the session ID, the access token and when it expires. createAuthFetch reads them to add the token to your requests, and your own request code can too.

status says where the session stands, and checking it tells TypeScript whether user is set:

statusMeaninguser
initializingThe saved session hasn't been read yet.null
authenticatedSigned in, with an access token that hasn't expired or been rejected.Set
refreshingSigned in, and replacing an access token that expired or was rejected.Set
lockedSigned in with locking, and the tokens wait for unlock().Set
unauthenticatedSigned out.null
unavailableThe access token couldn't be refreshed, or the saved session couldn't be read. error usually says why.null

Where the session is kept depends on where your app renders:

Where it's keptIn the browser or on the deviceOn the server
ForSingle-page apps and Expo appsTanStack Start, Next.js, React Router with server rendering
ClientcreateAuthcreateServerAuth, with createMirrorAuth in the browser
Saved inlocalStorage, sessionStorage, the iOS keychain, or Android's encrypted storageAn encrypted, httpOnly cookie
The refresh tokenStays in that storageSealed in the cookie, where page scripts can't read it

The hooks, the route guards and tab sync work the same in both. Storage says more about each kind of storage, and Server rendering about the cookie.

Next steps

  • Installation: install the package and find the guide for your setup.
  • Getting started: build a small app that signs in, stays signed in, and calls an API.
  • Server rendering: keep the session on the server with TanStack Start, Next.js or React Router.

On this page