Plan — Auth Update: Register & Sign-In for Comments

Intent

Add user registration and sign-in to bahalaka.com so only authenticated users can post and edit comments. Follow the exact auth pattern proven on lakemedical.org — NTRP wire protocol, Cloudflare Pages Functions as proxy, Pantheon C auth server via relay.

Scope

What Changes

Approach — Following the lakemedical.org Pattern

Phase 1 — Auth Infrastructure

Copy and configure the proven auth stack from lakemedical.org:

Phase 2 — Cloudflare Pages Functions

Wire proxy that routes auth requests to the relay:

Phase 3 — Comment System Update

Integrate auth into the existing Firestore comment engine:

Phase 4 — Comment Backend Migration

Move Firestore writes server-side to enforce auth:

Auth Flow (same as lakemedical.org)

StepActionComponents
1User clicks "Sign in to comment"Comment form → login.html redirect
2User registers: enters usernamelogin-gate.js → wire.js → /api/wire → relay → C auth server
3Server returns QR code (TOTP secret)C auth server (totp.c + qr.c) → NTRP response
4User scans QR with authenticator appGoogle Authenticator / Authy / 1Password
5User enters 6-digit TOTP codelogin-gate.js → nous_auth() → /api/wire → relay → verify
6Server returns session token (32 bytes)C auth server → sessionStorage['nous-session']
7Redirect back to site with session activeSession timer starts (15 min), comment forms enabled

Architecture Diagram

Request Flow

Browser (wire.js)
   │ NTRP binary frame
   ▼
Cloudflare Pages Function (/api/wire)
   │ HTTP POST (JSON)
   ▼
relay.3-nous.net:8080
   │ internal
   ▼
C Auth Server (gate.c → auth.c → totp.c → crypt.c)
   │
   ├── /register → generate TOTP secret + QR (qr.c)
   ├── /auth → verify TOTP code, mint session token
   └── /session → validate existing session

Browser (comment engine)
   │ Firestore REST API (reads)
   ▼
Firestore (bahalaka-website)

Browser (comment post/edit)
   │ authenticated request
   ▼
Cloudflare Pages Function (/api/comments)
   │ verify session with relay, then write
   ▼
Firestore (bahalaka-website)

Files to Copy from lakemedical.org

SourceDestinationChanges Needed
alumni/js/wire.jssite/js/wire.jsNone — generic NTRP
alumni/js/nous.jssite/js/nous.jsNone — generic auth client
alumni/js/login-gate.jssite/js/login-gate.jsUpdate redirect URL, styling references
alumni/css/auth-gate.csssite/css/auth-gate.cssAdapt to bahalaka color scheme
functions/api/wire.jsfunctions/api/wire.jsUpdate rate limits, CORS origins

New Files

FilePurpose
site/login.htmlStandalone login page (register + sign-in)
site/js/comment-guard.jsShows/hides comment forms based on session state
functions/api/comments.jsAuthenticated comment write proxy to Firestore
wrangler.tomlCloudflare Pages config

Constraints