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.

If your backend puts the user in a JWT access token, read it from there with fromAccessToken. signIn then takes tokens only, the refresh callback returns them, and the user changes whenever the token does:

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;
}) => .();
  • No updateUser. The token is the only source of the user, so call auth.refresh() to pick up a change.
  • The signature isn't verified. That suits a browser, which trusts a user object in a JSON response to the same degree. Pass { decode } as the third argument to verify or decode the token yourself.
  • Leave out claims that change on every refresh, such as exp, iat, or jti, so the user stays the same object across refreshes.
  • For backends you control. OAuth providers can change their access token format without notice.

The tanstack-start-spa and react-router-data examples read their user this way.