Skip to Content
Using the client

Users from the access token

Read the user from a JWT access token's claims, instead of from your backend's sign-in response.

Some backends answer a sign-in with tokens only, and put the user in the access token: a JWT whose claims hold the user's ID, email and roles. Then there's no user object to pass to signIn, and the user can change on every refresh. fromAccessToken reads the user from the token's claims instead.

This page shows how to set it up, what changes for the client, and how to verify the token on a server.

Read the user from the token

Pass fromAccessToken as the user option. Its first argument is a schema for the claims, and its second turns them into the user your app sees:

src/token-auth.ts
import {
  ,
  ,
  ,
} from "@monarcode/session-kit";
import {  } from "zod";

const  = .({ : .(), : .() });

export const  = ({
  : "my-token-app",
  // The claims are validated, then mapped to the user the app sees.
  : (, () => ({
    : .,
    : .,
  })),
  : (),
});

export const  = (: {
  : string;
  : string;
}) => .();

signIn takes tokens only, and your refresh callback returns tokens only. Without the second argument, the claims are the user.

What changes

Reading the user from the token changes a few things:

  • The user is read from every token: on sign-in, on each refresh, from tokens another tab saved, and on every load. Only the tokens are saved, so the user is read again each time the session is restored.
  • No updateUser. The token is the only source of the user, so call auth.refresh() to pick up a change.
  • Locking isn't available. A locked session shows its user without opening its tokens, so locking needs a user schema. createAuth throws INVALID_OPTIONS when you pass both.
  • For backends you control. OAuth providers can change their access token format without notice.

Keep the mapping stable

The mapping runs every time a token is read, so it has to give the same user for the same claims:

  • Keep it pure and synchronous. Don't fetch or read the clock in it.
  • Leave out claims that change on every refresh, such as exp, iat or jti. Then the user stays the same object across refreshes, and components that compare users don't re-render.

When the token can't be read

A token that isn't a readable JWT, or whose claims the schema rejects, fails with USER_VALIDATION_FAILED. What happens next depends on where the token came from:

The token came from…What happens
signInsignIn rejects, and the current session stays.
The saved session, on loadThe user is signed out, and state.error shows the error.
Another tabThe user is signed out, and state.error shows the error.
A refreshThe refresh fails with that error, and the session is kept, as after any failed refresh.

Verify the token on a server

By default, the claims are read without checking the token's signature. That suits a browser, which trusts a user object in a JSON response from its own backend just as much. On a server, such as with server sessions, verify the token: pass a decode function as the third argument:

src/verified-user.ts
import {  } from "@monarcode/session-kit";
import {  } from "zod";

// Your own check, with your JWT library: it throws for a bad signature.
declare function (: string): <unknown>;

export const  = (
  .({ : .(), : .() }),
  () => ({ : ., : . }),
  { :  },
);

decode returns the claims, and the schema checks them as before. It may return a promise, as verifyJwt does. In a browser, that stops a saved session from showing before the first render.

The tanstack-start-spa and react-router-data examples read their user from the access token.

Next steps

On this page