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.
Copy and configure the proven auth stack from lakemedical.org:
wire.js (NTRP pack/unpack, 173 lines) → site/js/nous.js (auth client, 162 lines) → site/js/login-gate.js (login page controller) → site/js/auth-gate.css → site/css/site/login.html — standalone login page with register + sign-inWire proxy that routes auth requests to the relay:
functions/api/wire.js from lakemedical.org → functions/api/wire.jswrangler.toml with Pages configNOUS_PSK secret on the Pages projectIntegrate auth into the existing Firestore comment engine:
Move Firestore writes server-side to enforce auth:
functions/api/comments.js — Pages Function that verifies session with relay, then writes to Firestore on behalf of the user| Step | Action | Components |
|---|---|---|
| 1 | User clicks "Sign in to comment" | Comment form → login.html redirect |
| 2 | User registers: enters username | login-gate.js → wire.js → /api/wire → relay → C auth server |
| 3 | Server returns QR code (TOTP secret) | C auth server (totp.c + qr.c) → NTRP response |
| 4 | User scans QR with authenticator app | Google Authenticator / Authy / 1Password |
| 5 | User enters 6-digit TOTP code | login-gate.js → nous_auth() → /api/wire → relay → verify |
| 6 | Server returns session token (32 bytes) | C auth server → sessionStorage['nous-session'] |
| 7 | Redirect back to site with session active | Session timer starts (15 min), comment forms enabled |
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)
| Source | Destination | Changes Needed |
|---|---|---|
| alumni/js/wire.js | site/js/wire.js | None — generic NTRP |
| alumni/js/nous.js | site/js/nous.js | None — generic auth client |
| alumni/js/login-gate.js | site/js/login-gate.js | Update redirect URL, styling references |
| alumni/css/auth-gate.css | site/css/auth-gate.css | Adapt to bahalaka color scheme |
| functions/api/wire.js | functions/api/wire.js | Update rate limits, CORS origins |
| File | Purpose |
|---|---|
| site/login.html | Standalone login page (register + sign-in) |
| site/js/comment-guard.js | Shows/hides comment forms based on session state |
| functions/api/comments.js | Authenticated comment write proxy to Firestore |
| wrangler.toml | Cloudflare Pages config |