# Build authenticated tic-tac-toe from the ground up

This is one continuous Redweb TSX and socket tutorial. It starts with server-side game rules, then adds a validated protocol, connected players, a live TSX board, authentication, one application lifecycle, and real acceptance tests.

It pins published **Redweb 0.16.5** and **redweb-client 0.3.1**. Run `npm ci` normally; no local checkout or package link is required.

Hello World, the shared counter, chat, dashboard, and combined HTTP/WebSocket server are deliberately not tutorial checkpoints. They remain independent generated examples, each with its own walkthrough, source, and tests in Redweb's versioned template documentation.

## Follow the build

Read the chapters in order:

1. `01-model.md` — initialize the project with Redweb and write the authoritative game model.
2. `02-protocol.md` — define validated join, move, resume, snapshot, and notice messages.
3. `03-handlers.md` — connect verified players to the rules without socket bookkeeping.
4. `04-page.md` — render the board in server TSX and attach `/match`.
5. `05-authentication.md` — add accounts and compose one application with `defineApp`.
6. `06-play.md` — play, reconnect, test, and understand the production boundary.

The archive is the finished reference project. To learn the construction rather than merely run prebaked files, begin with the `redweb init` command in Step 1 and add each listed file as its chapter introduces it.

## Run the completed reference

Use Node **22.13 or newer**. SQLite is experimental on Node 22; use a maintained Node release for deployment.

```sh
npm ci
npm run build
npm run add-user -- alice
npm run add-user -- bob
npm start
```

Save the random passwords printed by provisioning. There are no default passwords. Open **http://localhost:8181/login** in two separate browser profiles, sign in as different accounts, and join the same room. The first account is X and the second is O.

## How the finished files fit

- `src/game.ts` is the synchronous, server-authoritative game and bounded room store.
- `src/command-schemas.ts`, `messages.ts`, and `contract.ts` define the shared protocol.
- `src/handlers.ts` contains one decision per command. `src/players.ts` projects private state while Redweb owns connection membership, presence, and fan-out.
- `src/board.tsx` renders the reusable board. `src/routes.tsx` binds typed handlers directly to TSX and attaches the `/match` route.
- `src/server.tsx` composes protected pages, the socket route, and account resources. `src/app.tsx` is only the process entry point.
- `src/transport.ts` contains limits. `src/raw-client.ts` is an optional adapter for non-HTML clients.
- `src/shared/auth.ts`, `store.ts`, and `admin.ts` are introduced in the authentication chapter, so the game never depends on another example as its starting template.

There is no browser game bundle or handwritten socket controller. Redweb serves `redweb-client/live-html`, sends commands from the TSX bindings, and applies private server-rendered updates on `/match`.

## Configuration

Set `PORT`, `GAME_DATABASE` (default `data/game.sqlite`), and `GAME_ORIGIN` (an exact HTTP(S) origin without a trailing slash). With the default origin, use **localhost**, not `127.0.0.1`. In production, set `NODE_ENV=production` and `GAME_ORIGIN=https://your-domain.example`, terminate HTTPS at a trusted reverse proxy, and protect the SQLite volume and backups.

The `gameApp` factory provides isolated configuration for tests; it is not another Redweb entry-point convention. The executable remains:

```tsx
import { gameApp } from './server.js';

const app = gameApp();
await app.run();
```

## Tests and limits

```sh
npm test
npm run test:coverage
npm run test:browser
```

Tests use real HTTP, WebSockets, SQLite, password hashing, and headed Chrome. Coverage enforces 100% statements, branches, functions, and lines for the game, messages, schemas, handlers, players, raw adapter, transport, and board. This is not a whole-project coverage claim; the reused authentication recipe and emitted decorator helpers have their own verification boundaries.

The root Express override keeps `qs@6.16.0` selected until compatible upstream ranges include the fix. Recheck the dependency tree and audit before removing it.

Accounts persist, but matches are in-memory, single-process, capped at 100 rooms, and lost on restart. Room names are public identifiers rather than invitations. Add durable matches, private invitations, expiry, account recovery, backups, abuse controls, and monitoring before public production use.
