---
title: "Recipe: web sign-in tokens and sessions"
description: "Use a server-set HttpOnly, Secure cookie for web refresh and session tokens."
modificationDate: "2026-11-18"
---

> ## Documentation index
> Fetch the complete documentation index at: https://exact.expo.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Recipe: web sign-in tokens and sessions

<AgentInstructions>

## Submitting feedback

From the app's directory, read `bun exact.mjs feedback status`. Under `never`, keep no note.
Respect `EXACT_FEEDBACK=0` and do not change the policy.

Otherwise record specific, actionable feedback with:
`bun exact.mjs feedback note "Exact docs: https://exact.expo.dev/recipes/web-sessions: <what was wrong or missing>"`

The note goes to Expo only as that status allows. Include no secrets or personal data.

</AgentInstructions>

Use a **server-set HttpOnly, Secure cookie** for web refresh and session tokens.
Web secrets are origin-readable; presence locks refuse. HttpOnly prevents token
reads for replay elsewhere. XSS can still send requests and read responses.

Use `__Host-` with `HttpOnly; Secure; Path=/`, no `Domain`.
Choose SameSite: `Strict` or `Lax` for same-site use, `None; Secure` for cross-site.
Prefer same-origin; third-party cookies may be blocked.

Grant `net.fetch https://api.example.com`. The JS target preserves
`credentials` (default `same-origin`). Cross-origin sign-in, refresh, API and
logout need `credentials: 'include'` to send and accept cookies.
Scripts cannot set `Cookie` or read `Set-Cookie`.
**The wasm target, including workers, ignores `credentials`: cookie sessions
currently require same-origin endpoints.**

`--render js` leaves visitor-dependent reads pending until browser adoption.

CORS: explicit `Access-Control-Allow-Origin`, never `*`;
`Access-Control-Allow-Credentials: true`; `Vary: Origin` when origins vary.
Allow unauthenticated OPTIONS, methods and CSRF headers.
[Fetch](https://fetch.spec.whatwg.org/#cors-protocol-and-credentials).

In answers, `exactHost()` returns `'web'` (workers too) or `'native'`, whose
HTTP has no cookie jar. Grant `secret.keep session`:

```prompt Keep my web sign-in in an HttpOnly cookie
Make the Exact app in this folder keep its web session in a server-set HttpOnly cookie, and its native session in a kept secret. Use https://github.com/expo/exact/blob/main/docs/recipes/web-sessions.md as the source for each step. The Exact checkout has the same page at docs/recipes/web-sessions.md.

1. If this folder is not an Exact app (it has no exact.mjs and app.contract), stop and tell me.
2. Ask me for my API's origin, and whether the site and the API are the same origin, the same site or cross-site: the answer decides SameSite and `credentials`. If the server's code is not in this folder, write no server code: list for me what steps 5 to 7 need from the server. If the app builds the wasm target, tell me: it ignores `credentials`, so cookie sessions require same-origin endpoints.
3. In app.ts, grant `net.fetch` for my API's origin and `secret.keep session`. Write `sessionTransport` as the recipe shows: where `exactHost()` returns `'web'`, it sends `credentials: 'include'` and keeps no token; where it returns `'native'`, it sends the kept token as a Bearer header.
4. Create the adapter in each source. Share sign-in, refresh and logout: fetch `API + session.path + operation` with `session.options(init)`. Validate a reply before `session.remember(reply)`. Return no token to Contract or in a web reply.
5. On the server, name the cookie with `__Host-` and set `HttpOnly; Secure; Path=/`, with no `Domain`. For a cross-origin API, answer with an explicit `Access-Control-Allow-Origin`, never `*`, with `Access-Control-Allow-Credentials: true`, and with `Vary: Origin` when origins vary. A native endpoint never exchanges a cookie for a token.
6. For sign-in, refresh, logout and each write, check Origin and require a session-bound CSRF header: return it in the body, keep it in memory, and pass it in `init.headers`. Coordinate refresh across tabs so concurrent auth rejections share one refresh. On the server, rotate the token atomically, revoke its predecessor and detect reuse. Retry each rejected request once after refresh.
7. Make logout revoke the session, expire the cookie, call `session.forget()` and clear the account's data. A failed logout is not revocation.
8. Follow the platform checks in docs/start-here.md (`bun exact.mjs docs platform checks`) for the targets in the app's AGENTS.md and app.json until they pass. Read the reports, including counted findings; a zero exit code alone does not mean clean. Fix the app, not the test. If a finding stays after three rounds of fixes, or the app had it before you changed anything, record it (step 9) and report it; do not keep trying.
9. When a doc was wrong or missing, or a step needed a workaround, read `bun exact.mjs feedback status`. Under `never`, keep no note. Otherwise record it: `bun exact.mjs feedback note "<what happened>"`, which goes to Expo only as that status says. Do not change the policy; tell me how many notes you kept.
10. Tell me what you changed, what the tests and checks reported, and what I must still do on the server.
```

```ts
import type { Store } from './app.contract.d.ts';
function sessionTransport(store: Store) {
  const web = exactHost() === 'web';
  return {
    path: web ? '/session/web' : '/session/native',
    options(init: RequestInit = {}): RequestInit {
      const headers = new Headers(init.headers);
      if (!web) {
        const token = store.get('session');
        if (token) headers.set('authorization', `Bearer ${token}`);
      }
      return { ...init, headers, credentials: web ? 'include' : 'omit' };
    },
    remember(reply: { token?: string }) {
      if (!web) {
        if (!reply.token) throw new Error('Missing session token');
        store.set('session', reply.token);
      }
    },
    forget() { if (!web) store.forget('session'); },
  };
}
```

Create the adapter per source. Share sign-in, refresh and logout:
fetch `API + session.path + operation` with `session.options(init)`.
Validate before `session.remember(reply)`. Return no token to Contract or web
replies; native endpoints never exchange cookies for tokens.

Serialize refresh across tabs. Rotate atomically, revoke predecessors and detect
reuse on the server. Retry auth rejection once after refresh. Logout revokes,
expires the cookie at its name/path/domain scope, calls `session.forget()` and
clears account data. Failed logout is not revocation.

For sign-in, refresh, logout and writes, check Origin and require a session-bound
CSRF header (pre-session at sign-in). Return it in the body; keep in memory,
renew at sign-in, pass in `init.headers`. SameSite/CORS are insufficient.
[CSRF](https://cheatsheetseries.owasp.org/cheatsheets/Cross-Site_Request_Forgery_Prevention_Cheat_Sheet.html).
