# Hybrid setup (https://session-kit.pages.dev/docs/server-rendering/hybrid)

Sometimes your backend's sign-in has to run in the browser, such as through
a provider's SDK, or your pages call your API themselves. A hybrid setup
lets the browser do both, while the server still keeps the session in its
sealed cookie (encrypted with your secret, so only your server can read
it), refreshes it, and renders who is signed in.

Two things change from a server-rendered setup:

- **Signing in.** The browser gets the tokens from your backend and sends
  them to a server function, which seals them into the cookie with
  `session.signIn`.
- **Calling your API.** The session route (the route in your app that the
  browser's client calls to read the session and sign out) gives the browser
  the access token, and `createAuthFetch` sends it. The refresh token stays
  in the cookie.

This page builds on the [TanStack Start](https://session-kit.pages.dev/docs/setups/tanstack-start) guide.
[Other frameworks](#other-frameworks) lists what changes with Next.js and
React Router.

## Before you begin

- Follow the [TanStack Start](https://session-kit.pages.dev/docs/setups/tanstack-start) guide through
  step 7. This page replaces four of its files: `src/auth/server.ts`,
  `src/auth/functions.ts`, `src/auth/client.ts` and `src/sign-in.ts`. The
  rest stays as it is, including the sign-in route, which calls
  `src/sign-in.ts`.
- Your backend must allow requests from your site (CORS) for its sign-in
  and for the API calls the browser makes. Session kit's own requests go to
  your session route, on your site.

## 1. Let the session route serve the access token

`clientCredentials: true` lets the session route give the access token to
your own pages. Here's the guide's `src/auth/server.ts` with that option,
and without its `login`, which moves to the browser in the next step:

```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!,
  // The session route gives the access token to your own pages.
  clientCredentials: true,
  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);
```

## 2. Sign in with your backend in the browser

The browser calls your backend's sign-in, and gets the tokens and the user:

```ts title="src/backend.ts"
import { z } from "zod";

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

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

export type LoginResult = Awaited<ReturnType<typeof login>>;

/** Signs in with your backend, from the browser. */
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 }),
  });
  if (!response.ok) throw new Error(`Sign-in failed (${response.status})`);
  const body: unknown = await response.json();
  return { ...tokensSchema.parse(body), user: userSchema.parse(body) };
}
```

## 3. Send the tokens to the server

`session.signIn` seals what it's given, and never asks your backend. So the
server function decides what to trust. Check the tokens first, unless the
cookie's user is only ever shown.

### Check the tokens first

The server function takes the tokens only, and asks your backend whose they
are. A forged or expired access token is refused, and the cookie's user
comes from your backend, not from the browser. The refresh token isn't
checked until the first refresh:

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

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

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

export const signInFn = createServerFn({ method: "POST" })
  .validator(z.object({ accessToken: z.string(), refreshToken: z.string() }))
  .handler(async ({ data }) => {
    // Your backend proves the access token is real, and says whose it is.
    const response = await fetch(`${API}/auth/me`, {
      headers: { Authorization: `Bearer ${data.accessToken}` },
    });
    if (!response.ok) throw new Error("Your backend refused the token");
    const user = userSchema.parse(await response.json());
    return session.signIn({ ...data, user });
  });
```

The client's `signIn` takes what your backend's sign-in returned, and sends
only the tokens to `signInFn`:

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

import type { LoginResult } from "../backend";
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: ({ accessToken, refreshToken }: LoginResult) =>
      signInFn({ data: { accessToken, refreshToken } }),
  });
}

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

The form signs in with your backend, then with the client. The sign-in
page's guard sends the user on, so the form doesn't navigate:

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

/** 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(await login(username, password));
    return null;
  } catch (error) {
    return error instanceof Error ? error.message : "Could not sign in";
  }
}
```

With `fromAccessToken` as the server's `user` option, the server reads the
user from the access token instead. Give it a `decode` that verifies the
token's signature, and that's the check: see
[Verify the token on a server](https://session-kit.pages.dev/docs/client/users-from-tokens#verify-the-token-on-a-server).

### Pass the tokens without a check

If the cookie's user is only ever shown, the server function can take the
tokens and the user the browser sends, and seal them as they are:

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

import { userSchema } from "../backend";
import { session } from "./server";

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

export const signInFn = createServerFn({ method: "POST" })
  .validator(
    z.object({
      accessToken: z.string(),
      refreshToken: z.string(),
      user: userSchema,
    }),
  )
  .handler(({ data }) => session.signIn(data));
```

The client's `signIn` then sends everything your backend's sign-in
returned. `src/sign-in.ts` stays as it is:

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

import type { LoginResult } from "../backend";
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: (result: LoginResult) => signInFn({ data: result }),
  });
}

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

Anyone can then put any user in their own cookie, with tokens of their own.
That's safe only when the cookie's user is shown, never trusted:

- **Your backend authorizes every request** with the access token, and
  nothing on your server acts on `session.user` alone, such as reading
  another account's data by its `id`.
- **A forged token gets nowhere.** Your API refuses it. At the first
  refresh, your backend refuses its refresh token too, and a `refresh` that
  returns `null` for that, as this guide's does, signs the user out.

When in doubt, check the tokens first. It costs one request at sign-in.

## 4. Call your API from the browser

`createAuthFetch` from `@monarcode/session-kit/http` sends the access token
that the session route serves. Make one per client, since the client is made
once per page load:

```tsx title="src/orders.tsx"
import { useMemo, useState } from "react";

import { createAuthFetch } from "@monarcode/session-kit/http";
import { useAuthClient } from "@monarcode/session-kit/tanstack-router";

export function Orders() {
  const auth = useAuthClient();
  const authFetch = useMemo(
    () => createAuthFetch(auth, { baseUrl: "https://dummyjson.com/" }),
    [auth],
  );
  const [count, setCount] = useState<number | null>(null);

  async function load() {
    // DummyJSON's carts stand in for your orders.
    const response = await authFetch("auth/carts");
    if (!response.ok) throw new Error(`Orders failed (${response.status})`);
    const body: unknown = await response.json();
    const carts = (body as { carts?: unknown } | null)?.carts;
    setCount(Array.isArray(carts) ? carts.length : 0);
  }

  return (
    <div>
      <button onClick={() => void load().catch(() => {})}>Load orders</button>
      {count !== null && <p>{count} orders</p>}
    </div>
  );
}
```

After a 401, the client asks the session route for a new access token, and
the server refreshes with the cookie's refresh token.
[The access token in the browser](https://session-kit.pages.dev/docs/server-rendering/server-sessions#the-access-token-in-the-browser)
says how the browser holds the token.

ky, axios and openapi-fetch can send their requests through `authFetch`:
see [Other HTTP clients](https://session-kit.pages.dev/docs/client/other-http-clients). Server functions
can still call your API too, as the guide's
[Call your API](https://session-kit.pages.dev/docs/setups/tanstack-start#call-your-api) shows.

## Other frameworks

With Next.js or React Router, the steps are the same. What changes is where
each piece lives, and how the session reaches the browser after sign-in.

In the [Next.js](https://session-kit.pages.dev/docs/setups/next) guide's app:

- Add `clientCredentials: true` in `lib/auth/server.ts`.
- Have `signInAction` in `app/actions.ts` take the tokens, check them as in
  step 3, and call `session.signIn`.
- The client in `lib/auth/client.ts` already reads the session through
  `endpoint`, with no `revalidate`. Its `signIn` option calls
  `signInAction` with the tokens.
- Components get the client with `useAuthClient` from
  `@monarcode/session-kit/react`.

In the [React Router with server rendering](https://session-kit.pages.dev/docs/setups/react-router-ssr)
guide's app:

- Add `clientCredentials: true` in `app/auth/session.server.ts`.
- Give the sign-in route an action that takes the tokens, checks them as in
  step 3, and calls `auth(context).signIn`.
- After your backend's sign-in, submit the tokens to that action from the
  browser, such as with `useSubmit`. React Router runs the loaders again,
  and `useMirrorAuth` takes the new session.
- Components get the client with `useAuthClient` from
  `@monarcode/session-kit/react`.

## Things to know

- **The refresh token passes through page scripts during sign-in,** in
  your backend's sign-in response, on its way to your server. Session kit's
  client passes it on without keeping it, and never holds it again. A
  provider's SDK may keep its own copy: check its storage settings. If your
  backend's sign-in can run on the server, sign in there, as the setup
  guides do.
- **Signing out** works as in the guide's
  [Sign out](https://session-kit.pages.dev/docs/setups/tanstack-start#7-sign-out) step, through the
  session route. To end the session at your backend too, give
  `createServerAuth` a `revoke`.

## Next steps

- [Your backend decides what's allowed](https://session-kit.pages.dev/docs/security#your-backend-decides-whats-allowed):
  what the cookie's user can be trusted for.
- [The access token in the browser](https://session-kit.pages.dev/docs/server-rendering/server-sessions#the-access-token-in-the-browser):
  how page scripts get and renew the access token.
- [Other HTTP clients](https://session-kit.pages.dev/docs/client/other-http-clients): send ky, axios or
  openapi-fetch requests through `authFetch`.
- [Server sessions](https://session-kit.pages.dev/docs/server-rendering/server-sessions): the cookie, its
  secret and the limits every server-rendered setup shares.