web/README.md promised this was "three lines in router.tsx plus deleting the banner". It was nearly true. Every route is now behind RequireAuth: the gallery for any signed-in user, Devices and System for admin (SPEC 3.1). Roles are listed per route rather than granting admin everything implicitly, so widening one route cannot silently widen another - the same reasoning as api/userauth.require_role. The parts the README did not anticipate: A sign-out control. signOut() existed in AuthProvider and nothing called it, which was fine while the app was ungated and is not fine now: enforcing auth without a way to leave strands whoever signs in. It sits in the header with the theme and language switchers, and only renders when there is a session to end. lib/api.ts now carries the CSRF token, and fixing that surfaced a real bug in the scaffold. The fetch options spread `...init` AFTER `headers:`, so any caller passing headers replaced the whole object instead of merging into it. apiPost always sets Content-Type, so Accept was already being dropped on every POST - harmless, and invisible, because the server does not care. It stops being harmless the moment a header has to be there: the CSRF token would have been dropped on exactly the mutating requests that require one, and every POST would have answered 403 with nothing in the client to explain it. `...init` goes first now, with the reason written next to it. RequireAuth no longer shows the login form for the "unavailable" state. That state means the server answered 404 to /auth/session, which now only happens when a newer UI is served by an older server - so offering a form that 404s is the wrong answer. It says the versions do not match instead. auth.notEnforced is deleted from both catalogues rather than left unused, and .banner went with it, since that notice was its only caller. auth.failed was reworded: the server answers every sign-in failure identically - wrong password, unknown user, bad code, deactivated account - so the message has to cover all of them rather than inventing a distinction the server refuses to make. New keys in both en.ts and de.ts, which is enforced at build time. Verified through the Vite dev proxy, which is the path a browser actually takes rather than the one the tests take: 401 signed out, 401 for an admin without a TOTP code, 200 with one, both cookies arriving through the proxy, 403 on logout without the CSRF header with the session still alive after it, 204 with the header, 401 after. And POST /ingest still answers 401 rather than 403, because a camera sends no cookies and must never need a CSRF token. Docs brought in line: web/README.md said authentication was built but not enforced, which is now the opposite of true, and it now also says the two things needed before anyone can sign in - a user, and POORBIRD_SESSION_COOKIE_SECURE=false for plain-http development. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
139 lines
5.6 KiB
Markdown
139 lines
5.6 KiB
Markdown
# poorBird web
|
|
|
|
Phase 3 scaffolding: shell, themes, i18n and the auth plumbing. Built on the
|
|
dev machine and shipped to the server as static files — nothing here runs on
|
|
the server (SPEC §6).
|
|
|
|
```bash
|
|
cd web
|
|
npm install
|
|
npm run dev # http://localhost:5173
|
|
```
|
|
|
|
The dev server proxies `/api` to `http://127.0.0.1:8090`, so start the API
|
|
too (see [`../server/README.md`](../server/README.md)). Both must be running
|
|
or the System page shows "cannot reach the server", which is the honest
|
|
answer rather than a blank screen.
|
|
|
|
**Two things the server side needs before you can sign in:**
|
|
|
|
```bash
|
|
cd ../server
|
|
# 1. Secure cookies are dropped over plain http, and this dev server is http.
|
|
# Without this, login returns 200 and you are never signed in.
|
|
echo 'POORBIRD_SESSION_COOKIE_SECURE=false' >> .env
|
|
|
|
# 2. There is no sign-up page. Accounts are created on the server.
|
|
.venv/Scripts/poorbird user add me --role admin
|
|
```
|
|
|
|
`--role admin` enables TOTP and prints an `otpauth://` URI once — scan it
|
|
before pressing on, because the app will ask for a code and the URI is not
|
|
shown again. `--no-totp` skips it and says why that is a bad idea.
|
|
|
|
| Script | Does |
|
|
|---|---|
|
|
| `npm run dev` | Vite dev server with HMR |
|
|
| `npm run build` | typecheck, then production bundle into `dist/` |
|
|
| `npm run typecheck` | `tsc --noEmit` on its own |
|
|
| `npm run check:contrast` | assert WCAG contrast for all three themes |
|
|
|
|
> On Windows, Vite binds IPv6 `localhost`. Use `http://localhost:5173`, not
|
|
> `http://127.0.0.1:5173`.
|
|
|
|
---
|
|
|
|
## What is real and what is not
|
|
|
|
**Authentication is real and enforced.** `/auth/session`, `/auth/login` and
|
|
`/auth/logout` exist, so every route is now behind `RequireAuth`:
|
|
|
|
- the gallery needs any signed-in user; **Devices and System need `admin`**
|
|
(SPEC §3.1), so a `viewer` account sees a "no access" message there
|
|
- TOTP is required whenever the account has a secret, which `user add --role
|
|
admin` sets up by default
|
|
- signing out is in the header, next to the theme and language switchers
|
|
- **there is still no development bypass**, and there never was one — a bypass
|
|
that works is the kind of code that survives into production
|
|
|
|
The guard here decides what to *render*. It is not access control: the server
|
|
authorises every request on its own, and deleting `RequireAuth.tsx` would
|
|
expose no data, it would only render pages whose requests then 401.
|
|
|
|
**Only `GET /health` and `/auth/*` return real data.** So the **System** page
|
|
shows live values and the others do not yet:
|
|
|
|
Gallery and Devices render an honest empty state. They are deliberately not
|
|
filled with mock rows — fake data hides exactly the layout problems that real
|
|
data exposes. The capture and device listing endpoints are the next server
|
|
work; the six captures already in the dev database have nothing to serve them.
|
|
|
|
---
|
|
|
|
## Conventions
|
|
|
|
**No user-facing string literal in a component. Ever.** Every one goes
|
|
through `useT()`. `src/i18n/en.ts` defines the key set; `de.ts` is typed as
|
|
`Catalogue`, so a missing or misspelled German key fails `npm run build`
|
|
rather than showing English to a German user at runtime.
|
|
|
|
**No colour literal outside `src/styles/tokens.css`.** Components reference
|
|
tokens, which is what makes a fourth theme a single CSS block. Run
|
|
`npm run check:contrast` after touching any colour — it caught three failing
|
|
`--border-strong` values the first time it ran.
|
|
|
|
**`lib/api.ts` is the only place that calls `fetch`,** and the only place that
|
|
knows about CSRF. It reads the `poorbird_csrf` cookie and echoes it in
|
|
`X-CSRF-Token` on every POST/PUT/PATCH/DELETE; the server compares it against
|
|
the token stored on the session row. A new mutating call needs nothing extra,
|
|
which is the point of there being one such place.
|
|
|
|
**Themes** are `data-theme` on `<html>`: `light`, `dark`, `blue` (dark navy).
|
|
An inline script in `index.html` applies the stored choice before first paint
|
|
to avoid a flash of the wrong theme; it must stay in sync with
|
|
`theme/ThemeProvider.tsx` — same storage key, same attribute, same default.
|
|
Once accounts exist, `users.theme` becomes the source of truth and
|
|
localStorage stays as the pre-login default.
|
|
|
|
---
|
|
|
|
## Layout
|
|
|
|
```
|
|
src/
|
|
main.tsx providers, in order
|
|
router.tsx the whole route table, one flat file
|
|
i18n/ en.ts (source of truth), de.ts (typed against it)
|
|
theme/ data-theme on <html>
|
|
auth/ provider, login form, route guard (not wired yet)
|
|
lib/api.ts the only place that calls fetch
|
|
components/ layout and shared pieces
|
|
pages/ one per route
|
|
styles/ tokens.css (themes), global.css (everything else)
|
|
scripts/check-contrast.mjs
|
|
```
|
|
|
|
---
|
|
|
|
## Notes on the stack
|
|
|
|
**TypeScript is strict**, including `noUncheckedIndexedAccess` and
|
|
`verbatimModuleSyntax`. Learning TypeScript with the checks off teaches a
|
|
different language than the one everyone else writes. The error messages say
|
|
what to do.
|
|
|
|
**i18n is hand-written, not `react-i18next`.** Two languages and a few hundred
|
|
strings do not need a library, and the typed catalogue gives compile-time
|
|
completeness that i18next does not provide by default. If real plural grammar
|
|
is ever needed, ICU MessageFormat is the upgrade and the call sites keep their
|
|
shape.
|
|
|
|
**TanStack Router with code-based routes**, per SPEC §6, rather than the
|
|
file-based convention — one visible route table, no codegen step. Its payoff
|
|
is that `<Link to="/devices">` is typechecked, so a typo in a path is a build
|
|
error instead of a 404 found later.
|
|
|
|
**No ESLint yet.** `tsc --noEmit` catches the important class of mistakes;
|
|
ESLint is worth adding but its flat-config setup is a distraction during
|
|
scaffolding.
|