# TanStack Start (https://session-kit.pages.dev/docs/setups/tanstack-start)

Your TanStack Start app renders pages on the server, and you want the first
response to show the signed-in page, with no "Checking session…" state and no
flash of signed-out content. To do that, the server has to hold the session.
In this guide, you keep it in an encrypted cookie: the server signs in,
refreshes the tokens before rendering, and sends the signed-in page first.

The browser gets a small client that mirrors the session the server rendered,
and the [TanStack Router hooks and guards](https://session-kit.pages.dev/docs/setups/tanstack-router) work
with it. [Server rendering](https://session-kit.pages.dev/docs/server-rendering) explains how the pieces
fit. A complete app is in
[`examples/tanstack-start-ssr`](https://github.com/monarcode/session-kit/tree/main/examples/tanstack-start-ssr).

If your Start app runs in SPA mode, where the browser keeps the session,
follow
[TanStack Start in SPA mode](https://session-kit.pages.dev/docs/setups/tanstack-router#tanstack-start-in-spa-mode)
instead.

## Before you begin

- Node.js 22.12 or newer, which TanStack Start needs. See
  [Compatibility](https://session-kit.pages.dev/docs/reference/compatibility) for the other versions.
- A secret of 32 or more random characters to seal the cookie, such as the
  output of `openssl rand -base64 32`, set as `SESSION_SECRET` on the server.
  `vite build` doesn't need it.
  [The secret](https://session-kit.pages.dev/docs/server-rendering/server-sessions#the-secret) says how to
  keep and rotate it.
- The code signs in with [DummyJSON](https://dummyjson.com/docs/auth)'s test
  backend, as [Getting started](https://session-kit.pages.dev/docs/getting-started) does. Replace its URLs
  with your backend's.

## Installation

To install the packages, run the following command:

```npm
npm install @monarcode/session-kit @tanstack/react-start @tanstack/react-router zod
```

## 1. Create the server engine

The server engine keeps the session in a cookie sealed with your secret, and
refreshes the tokens with your backend. `createServerAuth` from
`@monarcode/session-kit/server` makes it, and `startAuth` gives you the session
of the request Start is handling. Create both in a server-only module, so Start
refuses to bundle it for the browser:

```ts title="src/auth/server.ts"
import "@tanstack/react-start/server-only";
import { createServerAuth } from "@monarcode/session-kit/server";
import { startAuth } from "@monarcode/session-kit/tanstack-start/server";
import { z } from "zod";

export const API = "https://dummyjson.com";

const userSchema = z.object({ id: z.number(), firstName: z.string() });
const tokens = z.object({ accessToken: z.string(), refreshToken: z.string() });

export const serverAuth = createServerAuth({
  name: "app",
  user: userSchema,
  secret: process.env.SESSION_SECRET!,
  refresh: async ({ refreshToken, signal }) => {
    const response = await fetch(`${API}/auth/refresh`, {
      method: "POST",
      signal,
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({ refreshToken }),
    });
    // DummyJSON rejected the refresh token: sign out.
    if (response.status === 401 || response.status === 403) return null;
    if (!response.ok) throw new Error(`Refresh failed (${response.status})`);
    return tokens.parse(await response.json());
  },
});

/** The session of the request Start is handling, found on each call. */
export const session = startAuth(serverAuth);

/** Signs in with your backend, from the server. */
export async function login(username: string, password: string) {
  const response = await fetch(`${API}/auth/login`, {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ username, password }),
  });
  const body: unknown = await response.json().catch(() => null);
  if (!response.ok) {
    // The backend says why, such as wrong credentials, in `message`.
    const failure = z.object({ message: z.string() }).safeParse(body);
    throw new Error(
      failure.success
        ? failure.data.message
        : `Sign-in failed (${response.status})`,
    );
  }
  return { ...tokens.parse(body), user: userSchema.parse(body) };
}
```

`session` finds the request each time it's called, so one object, made at
module level, serves every request. [Server
sessions](https://session-kit.pages.dev/docs/server-rendering/server-sessions) covers the engine's other
options, the cookie, and signing out.

## 2. Add the session route and server functions

The browser's client signs out and gets the access token through a session
route: a route in your app that the browser's client calls.
`sessionRoute` from `@monarcode/session-kit/tanstack-start/server` makes its
`GET` and `POST` handlers. Start leaves a route's `server.handlers` out of the
browser bundle, and `serverAuth` with them:

```ts title="src/routes/api/session.ts"
import { sessionRoute } from "@monarcode/session-kit/tanstack-start/server";
import { createFileRoute } from "@tanstack/react-router";

import { serverAuth } from "../../auth/server";

export const Route = createFileRoute("/api/session")({
  server: { handlers: sessionRoute(serverAuth) },
});
```

The session route answers your own site's pages only, so another site can't
sign the user out: see [The session route](https://session-kit.pages.dev/docs/security#the-session-route).

Two server functions do the rest. One signs in with your form's input. The
other reads the session, because `loadSession` reads it while Start renders on
the server, where the client can't call a route on its own site:

```ts title="src/auth/functions.ts"
import { createServerFn } from "@tanstack/react-start";
import { z } from "zod";

import { login, session } from "./server";

export const getSessionFn = createServerFn().handler(() =>
  session.getSession(),
);

export const signInFn = createServerFn({ method: "POST" })
  .validator(z.object({ username: z.string(), password: z.string() }))
  .handler(async ({ data }) => {
    const tokens = await login(data.username, data.password);
    return session.signIn(tokens);
  });
```

`getSession` and `signIn` resolve `{ sessionId, user }`, never tokens, so
their results are safe to send to the browser. Start's compiler replaces each
handler with a request in the browser bundle, and leaves `session` out of it.

## 3. Refresh before rendering

The server refreshes tokens that are about to expire before any route or
server function reads them, while the response can still set the cookie.
After Start starts streaming a page, a cookie set later is lost, and so are
the new tokens. `refreshSession` from
`@monarcode/session-kit/tanstack-start/server` does the refresh, in Start's
own request middleware:

```ts title="src/start.ts"
import { refreshSession } from "@monarcode/session-kit/tanstack-start/server";
import {
  createCsrfMiddleware,
  createMiddleware,
  createStart,
} from "@tanstack/react-start";

import { serverAuth } from "./auth/server";

const sessionMiddleware = createMiddleware({ type: "request" }).server(
  refreshSession(serverAuth),
);

export const startInstance = createStart(() => ({
  requestMiddleware: [
    createCsrfMiddleware({
      filter: ({ handlerType }) => handlerType === "serverFn",
    }),
    sessionMiddleware,
  ],
}));
```

Start bundles `src/start.ts` for the browser too, but strips what's inside
`.server()`, so `serverAuth` stays on the server.

`refreshSession` refreshes access tokens that expire within 60 seconds. For
tokens that live a minute or less, pass a smaller `within`, such as
`refreshSession(serverAuth, { within: 15 })`.
[Refresh before rendering](https://session-kit.pages.dev/docs/server-rendering/server-sessions#refresh-before-rendering)
says what happens when a refresh fails.

`createCsrfMiddleware` protects your server functions, because an app that
defines `createStart` has no CSRF check until it adds one. Here's what it
covers:

- Its `filter` limits the check to server-function calls. Without the filter,
  it also refuses page loads from links on other sites and from the address
  bar.
- Filtered this way, it doesn't check server routes. `sessionRoute` refuses
  other sites itself. A server route of your own that changes the session
  must check too: see
  [Check your own routes](https://session-kit.pages.dev/docs/security#check-your-own-routes).

## 4. Create the browser client

The client that routes and components use comes from `createMirrorAuth`. It
mirrors the session the server read, and calls your server functions and the
session route. Create it in a function, not at module level:

```ts title="src/auth/client.ts"
import { createMirrorAuth } from "@monarcode/session-kit";

import { getSessionFn, signInFn } from "./functions";

export function createAppAuth() {
  return createMirrorAuth({
    name: "app",
    session: null, // filled by loadSession, then by hydration
    endpoint: "/api/session",
    revalidate: () => getSessionFn(),
    signIn: (data: { username: string; password: string }) =>
      signInFn({ data }),
  });
}

export type AppAuth = ReturnType<typeof createAppAuth>;
```

Let TypeScript infer the client's types from its options. With
`createMirrorAuth<User>(…)`, `signIn` would be left out.

The Router creates the client in `getRouter`, which Start calls once per
request on the server and once in the browser. `connectSession` sends the
session the server read to the browser with the page, and runs the guards
again when the session changes there:

```tsx title="src/router.tsx"
import { connectSession } from "@monarcode/session-kit/tanstack-start";
import { createRouter } from "@tanstack/react-router";

import { createAppAuth } from "./auth/client";
import { routeTree } from "./routeTree.gen";

export function getRouter() {
  const auth = createAppAuth();
  const router = createRouter({ routeTree, context: { auth } });
  connectSession(router);
  return router;
}

declare module "@tanstack/react-router" {
  interface Register {
    router: ReturnType<typeof getRouter>;
  }
}
```

A `dehydrate` and a `hydrate` of your own, such as for a query cache, go to
`createRouter` as usual. `connectSession` keeps them.

## 5. Load the session, then guard routes

The root route's `beforeLoad` reads the request's session into the client
with `loadSession`, before any guard runs. In the browser, the session that
came with the page is already there, so it doesn't call the server. The root
route also renders the document, with `<Scripts />` so the page hydrates, and
an error component for when the session can't be read:

```tsx title="src/routes/__root.tsx"
import type { ReactNode } from "react";

import { loadSession } from "@monarcode/session-kit/tanstack-start";
import {
  createRootRouteWithContext,
  HeadContent,
  Outlet,
  Scripts,
  useRouter,
  type ErrorComponentProps,
} from "@tanstack/react-router";

import type { AppAuth } from "../auth/client";

export const Route = createRootRouteWithContext<{ auth: AppAuth }>()({
  beforeLoad: ({ context }) => loadSession(context.auth),
  shellComponent: Document,
  errorComponent: SessionError,
  component: Outlet,
});

function Document({ children }: { children: ReactNode }) {
  return (
    <html lang="en">
      <head>
        <HeadContent />
      </head>
      <body>
        {children}
        <Scripts />
      </body>
    </html>
  );
}

function SessionError({ error }: ErrorComponentProps) {
  const router = useRouter();
  return (
    <div role="alert">
      <p>{error instanceof Error ? error.message : "Something went wrong"}</p>
      {/* Runs loadSession again. */}
      <button onClick={() => void router.invalidate()}>Retry</button>
    </div>
  );
}
```

When the server can't read the session, such as after a failed refresh,
`loadSession` rejects and `SessionError` shows. Its button calls
`router.invalidate()`, which runs `loadSession` again.

Guards are the ones from `@monarcode/session-kit/tanstack-router`. On the
server, a guard's redirect is a real HTTP redirect:

```tsx title="src/routes/_authenticated.tsx"
import {
  requireSession,
  SessionOutlet,
} from "@monarcode/session-kit/tanstack-router";
import { createFileRoute } from "@tanstack/react-router";

export const Route = createFileRoute("/_authenticated")({
  beforeLoad: ({ context, location }) =>
    requireSession(context.auth, { location }),
  component: () => <SessionOutlet pending={<p>Loading…</p>} />,
});
```

A signed-out user who opens a page under `_authenticated`, such as `/orders`,
lands on `/login?redirectTo=%2Forders`.
[Guard private routes](https://session-kit.pages.dev/docs/setups/tanstack-router#3-guard-private-routes)
explains `SessionOutlet`. This route needs no error component of its own:
`loadSession` reports a failed read first, in the root route.

Guards decide what a page shows. A server function that returns private data
must check the session itself, with `session.getSession()` or
`session.credentials.get()`, as `getOrdersFn` does in
[Call your API](#call-your-api).

## 6. Sign in

The client's `signIn` sends what it's given to your `signInFn`, then takes the
session it returns, runs the guards again and tells other tabs. A small helper
turns a failure into the message to show:

```ts title="src/sign-in.ts"
import type { AppAuth } from "./auth/client";

/** Signs in, and resolves the message to show when it fails. */
export async function signIn(
  auth: AppAuth,
  username: string,
  password: string,
): Promise<string | null> {
  try {
    await auth.signIn({ username, password });
    return null;
  } catch (error) {
    // What signInFn threw, such as your backend's message for a wrong
    // password.
    return error instanceof Error ? error.message : "Could not sign in";
  }
}
```

The sign-in route has the `redirectIfSignedIn` guard, so a signed-in user goes
on to `redirectTo`. Its form gets the client with `useAuthClient`, from Router
context:

```tsx title="src/routes/login.tsx"
import { useState, type FormEvent } from "react";

import { safeReturnTo } from "@monarcode/session-kit";
import {
  redirectIfSignedIn,
  useAuthClient,
} from "@monarcode/session-kit/tanstack-router";
import { createFileRoute } from "@tanstack/react-router";

import { signIn } from "../sign-in";

export const Route = createFileRoute("/login")({
  validateSearch: (search: Record<string, unknown>) => ({
    redirectTo: safeReturnTo(search.redirectTo),
  }),
  beforeLoad: ({ context, search }) =>
    redirectIfSignedIn(context.auth, { redirectTo: search.redirectTo }),
  component: Login,
});

function Login() {
  const auth = useAuthClient();
  const [error, setError] = useState<string | null>(null);

  async function submit(event: FormEvent<HTMLFormElement>) {
    event.preventDefault();
    const form = new FormData(event.currentTarget);
    const username = String(form.get("username"));
    setError(await signIn(auth, username, String(form.get("password"))));
  }

  return (
    <form onSubmit={(event) => void submit(event)}>
      <label>
        Username
        <input name="username" autoComplete="username" />
      </label>
      <label>
        Password
        <input
          name="password"
          type="password"
          autoComplete="current-password"
        />
      </label>
      {error && <p role="alert">{error}</p>}
      <button>Sign in</button>
    </form>
  );
}
```

The form doesn't navigate after a sign-in. `connectSession` runs the guards
again, and `redirectIfSignedIn` sends the user on. To try it, sign in as
`emilys` with the password `emilyspass`. A wrong password shows DummyJSON's
own message, which `login` threw.

In this guide, the server signs in with your backend, so the user in the
cookie comes from the backend. If the browser has to sign in with the backend
itself, for example through a provider's SDK, have `signInFn` take the tokens
instead, as [Hybrid setup](https://session-kit.pages.dev/docs/server-rendering/hybrid) shows.

## 7. Sign out

The client's `signOut()` signs out on the server through the session route,
which clears the cookie and calls your `revoke` option, if `createServerAuth`
has one. It then signs out this tab and tells the others. The guards run
again, so `requireSession` sends the user to the sign-in page. Put a button
like this one on any page under `_authenticated`:

```tsx title="src/sign-out.tsx"
import { useAuthClient } from "@monarcode/session-kit/tanstack-router";

export function SignOutButton() {
  const auth = useAuthClient();
  // A failed sign-out keeps the session, and shows in `state.error`.
  return (
    <button onClick={() => void auth.signOut().catch(() => {})}>
      Sign out
    </button>
  );
}
```

Without `revoke`, a copy of the cookie made before sign-out still works: see
[Sign-out](https://session-kit.pages.dev/docs/server-rendering/server-sessions#sign-out).

## Call your API

Your server functions call your API with `createAuthFetch` from
`@monarcode/session-kit/http`. Given `session`, it sends the access token of
the request Start is handling, refreshed first if it has expired. Make it
once, in a server-only module:

```ts title="src/auth/auth-fetch.ts"
import "@tanstack/react-start/server-only";
import { createAuthFetch } from "@monarcode/session-kit/http";

import { API, session } from "./server";

export const authFetch = createAuthFetch(session, { baseUrl: `${API}/` });
```

Then call it in a server function. This one checks the session first, so it
can ask for the signed-in user's orders:

```ts title="src/orders.ts"
import { createServerFn } from "@tanstack/react-start";
import { z } from "zod";

import { authFetch } from "./auth/auth-fetch";
import { session } from "./auth/server";

const orders = z.object({ carts: z.array(z.object({ id: z.number() })) });

export const getOrdersFn = createServerFn().handler(async () => {
  const current = await session.getSession();
  if (!current) throw new Error("Sign in to see your orders");
  const response = await authFetch(`auth/carts/user/${current.user.id}`);
  if (!response.ok) throw new Error(`Orders failed (${response.status})`);
  return orders.parse(await response.json()).carts;
});
```

When the user is signed out, `authFetch` rejects with `UNAUTHENTICATED`. When
your API answers 401, it gets a new access token and saves it in the cookie.
It sends a `GET` or `HEAD` again with the new token, as
[After a 401](https://session-kit.pages.dev/docs/client/requests#after-a-401) explains.

With an HTTP client of your own, such as ky or axios, see
[Other HTTP clients](https://session-kit.pages.dev/docs/client/other-http-clients). A client without a
`fetch` option reads the token with `session.credentials.get()`.

To call your API from the browser too, turn on `clientCredentials` in
`createServerAuth`: see
[The access token in the browser](https://session-kit.pages.dev/docs/server-rendering/server-sessions#the-access-token-in-the-browser).
The example app does this.

## Things to know

- **One session per request.** Each call to `session` reads the request Start
  is handling, and calls during one request share what it read and one
  refresh. Outside a request, such as in the browser, its methods reject with
  `INVALID_OPTIONS`, and `isCurrent` and `state` throw it.
- **Caching.** `refreshSession` sends `Cache-Control: private, no-store`
  with each response that doesn't set its own, so shared caches keep no
  user's page: see
  [Refresh before rendering](https://session-kit.pages.dev/docs/server-rendering/server-sessions#refresh-before-rendering)
  for `cacheControl`.
- **Prerendering.** A prerendered page renders at build time, without a
  cookie, so it shows the signed-out state. Don't prerender pages that read
  the session.
- **Other tabs.** Signing in or out in one tab reaches the others, which ask
  the server for the session and run the guards again: see
  [How tabs stay in step](https://session-kit.pages.dev/docs/server-rendering#how-tabs-stay-in-step).
- **Deploying.** Several server instances, the cookie's size, and plain http
  during development are covered in
  [Server sessions](https://session-kit.pages.dev/docs/server-rendering/server-sessions).

## Next steps

- [Server sessions](https://session-kit.pages.dev/docs/server-rendering/server-sessions): the cookie, the
  secret, signing out, and the limits every server-rendered setup shares.
- [Hybrid setup](https://session-kit.pages.dev/docs/server-rendering/hybrid): sign in from the browser
  and call your API there, with the session still on the server.
- [Using the client](https://session-kit.pages.dev/docs/client): what the browser's client can do.
- [Use Effect on the server](https://session-kit.pages.dev/docs/effect/server): run Effects with a
  request's session.