Skip to Content
Configuring the client

User schemas

How the user schema checks every user the client accepts, and what restoring a session asks of it.

Your backend answers a sign-in with the user's fields, and your components render them. The schema you pass as createAuth's user option sits between the two: it checks each user the client accepts, and gives the user its type in your code.

This page shows when the schema runs, the two rules it must follow so that a saved session can be restored, and how to change it without signing anyone out.

Any Standard Schema library works, such as Zod, Valibot or ArkType. If your backend puts the user in the access token instead, see Users from the access token.

The user's type

createAuth infers the user's type from the schema's output. With the schema from Getting started, hover user to see it:

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

import {  } from "./auth.js";

const { user } = ..();
const user: {
    id: number;
    username: string;
    email: string;
    firstName: string;
} | null
// The same types, named for the rest of your code. export type = <typeof >; export type = <typeof >;

user is null unless the session is authenticated, refreshing or locked, and checking status narrows it. To type the hooks from @monarcode/session-kit/react, register the client one time, as Getting started shows.

When the schema runs

The client runs the schema on every user it accepts:

  • At sign-in.
  • When updateUser replaces the user.
  • After a refresh that returns a user.
  • When it restores a saved session.
  • When another tab saves a session.

It keeps a frozen copy of each user it accepts. To change the user, call updateUser.

An asynchronous schema, such as one with an async refinement, has 15 seconds to answer each check, and a check that takes longer fails. During a refresh, the check shares the refresh's 15 seconds. It also means a saved session can't be restored before the first render, as the Introduction explains: the app shows initializing first.

Rules for restoring

The client saves the user beside the tokens, and runs the schema on it again when it restores the session. That sets two rules for the schema:

  • Its output is a plain JSON object. Dates, Maps and class instances can't be saved.
  • It accepts its own output and returns it unchanged. A one-way transform, such as one that renames a field, breaks this rule.

A schema that breaks either rule is rejected with USER_VALIDATION_FAILED the first time it accepts a user, usually at sign-in. That way the problem shows up right away, not as users being signed out on their next reload.

Change the schema safely

A deploy can change the schema so that it rejects users saved before it. When the client restores such a user, it signs them out and clears the saved session, with USER_VALIDATION_FAILED in state.error. The user then signs in again.

To add a field without signing anyone out, make it optional or give it a default. A saved user without the field still passes, and a default fills it in on restore.

A user that another tab saved, and that this tab's schema rejects, signs every tab out the same way. That can happen while two versions of your app are open in different tabs. Errors covers this and the other saved sessions the client can't use.

Next steps

On this page