Visitor authentication + per-visitor data
Visitor authentication + per-visitor data
Resolves #289.
Heads up on scope: this is large (~10,700 lines, 47 new files) because it bundles two inseparable layers — the auth foundation (Phases 1–3) and the per-visitor data framework that depends on it. Splitting would force landing an unused foundation first. Per guidance from @Nik0w, I've already reconciled this against #305 (real-time collab) — see "Conflict with #305" below.
What this adds
Instatic's auth is admin-only today, so member portals, gated content, and personalised visitor pages aren't possible. This adds the visitor side fully decoupled from admin auth.
1. Visitor authentication (Phases 1–3)
- Register / login / logout, Argon2id passwords
- Separate session cookie + middleware — different name (
instatic_visitor_session), different tables, different code. Zero runtime coupling to admin auth, enforced by an architecture-gate test - Rate limiting + exponential lockout, CSRF origin checks
- Member groups + page-level access control (per-page group gating)
- Password reset, account deletion
- Admin Members management surface (visitors, roles, groups)
- Built-in login/register pages with a server-rendered fallback when no custom page exists
2. Per-visitor data framework
- Site-builder-defined custom profile fields (e.g. "School name") configured in Settings → Members
- Two
perVisitorloop sources that drive the existing holes:visitor.current— "Welcome back, {name}",{schoolName}, etc.visitor.owned-rows— per-member listings ("Your tenders") of rows the visitor owns
- Form submissions auto-stamp the owning visitor on opt-in tables (
capturesVisitorOwner) - Custom profile fields surface in the
{}binding picker (no literal token typing) - Admin can edit per-visitor profile values + copy the stable Member ID (cross-system identifier)
- IDOR-safe by construction: visitor identity derives only from the validated session cookie, never from request input — locked by an architecture-gate test + unit/integration tests
Design principles honoured
- 95%+ of published pages stay static files on disk — auth adds ~0.1ms to the hot path (cached config lookup + cookie parse that returns null for unprotected pages)
- Minimal core diff — all visitor logic in new files (
server/visitor-auth/,src/core/loops/sources/visitor*); ~20 lines inserver/index.ts/router.ts perVisitorholes areno-store— content never bakes into static HTML; the placeholder does
Design docs
Full rationale in docs/proposals/visitor-auth/ (start with README.md → PRD.md):
- PRD.md — problem statement, user stories, full technical architecture
- PRD-REVIEW.md — the review cycle that shaped the design
- ARCHITECTURE.md — resolved decisions + file map + rebase notes
- PER-VISITOR-DATA-SPEC.md — the data-framework spec
Conflict with #305 (real-time collab) — already resolved
Per maintainer guidance, I checked #305 first. It's based on the same commit (d97cac23), so no rebase skew. Of 17 shared files, only 6 conflicted, all resolved additively:
- Migrations: #305's
022_collab_documents/023_collab_document_generationkeep their numbers; my visitor migrations renumbered022–027 → 024–029 - Loop sources: both
EntryFieldSource(#305) andvisitor.current/visitor.owned-rowsregistered DataPage: both #305's publish/draft flow and my page-access handler kept- Plus minor import/comment reconciliations
This branch merges #305 in, so reviewing against a collab-carrying tree won't surface conflicts.
Verification
tsc -bclean- Full test suite at parity with clean
upstream/main— zero regressions; the only failures are pre-existing flakes confirmed identical on upstream (capability-route-matrix, CMS plugin handlers, the 50s circular-deps timeout, PP-25 / step-up under parallel load) - Dedicated architecture-gate test for IDOR isolation + unit/integration tests for both loop sources, the cookie resolver, and profile fields
One open UX decision — would value a steer
I kept visitor management as a separate Members area rather than folding it into the existing Users menu — purely for simplicity. On reflection, unifying under one Users menu (admins and members as different "kinds") may be cleaner. I held off because it touches the existing Users surface. Happy to do that refactor before merge or as a follow-up — your call.
Transparency
Built with AI assistance; I reviewed, tested, and verified every change myself against the full suite. Happy to walk through any part on a call if that's easier than a big-diff review.
The commits are squashed onto upstream-rebase for review; I can re-split into the original ~20 if you'd prefer history. I can also rebase again onto whatever main looks like by the time you get to this.