Skip to content

Replace demo auth with better-auth#52

Merged
dashprotocol merged 1 commit into
mainfrom
feat/H-010-real-auth
May 28, 2026
Merged

Replace demo auth with better-auth#52
dashprotocol merged 1 commit into
mainfrom
feat/H-010-real-auth

Conversation

@dashprotocol
Copy link
Copy Markdown
Owner

@dashprotocol dashprotocol commented May 28, 2026

Summary

  • Replaces hardcoded demoAuth middleware with real
    cookie-based session auth using better-auth (Prisma
    adapter, email/password provider). All protected
    routes return 401 without a valid session;
    cross-patient access returns 403.
  • Establishes the first production deploy procedure:
    infra/deploy.sh handles build → migrate → seed →
    systemd restart → health poll in one command. Runbook
    updated with the restore evidence gate as a
    mandatory pre-start step.
  • Closes deploy gate D1.

What changed

Backend

  • better-auth mounted at /api/auth/*; Session,
    Account, Verification models added via migration
  • sessionAuth.ts replaces demoAuth.ts:
    requireAuth + requirePatientAccess + per-route
    IDOR ownership checks on all ID-based endpoints
  • Comments route: all provided entity IDs validated
    (not just the first) to prevent cross-tenant writes
  • Upload route: ownership check moved after multer
    (multipart body now available); file cleanup on all
    non-success exits
  • Seed: users created via auth.api.signUpEmail for
    correct scrypt hashing; stable hardcoded IDs for
    idempotent upserts; Account backfill for pre-auth
    users

Frontend

  • AuthProvider + useAuth() hook;
    ProtectedRoute; LoginPage with
    fetchOptions.onSuccess redirect (race-free,
    library-idiomatic)
  • All page queries gate on user.patientId;
    AppLayout logout button

Infra

  • infra/deploy.sh: build order fixed (full npm ci
    before tsc/prisma), migration hard-fails on
    error, restore evidence gate, 30s health poll
  • infra/systemd/havenhold-api.service:
    After=postgresql.service, EnvironmentFile from
    .env
  • deploy.sh validates VITE_AUTH_BASE_URL and
    VITE_API_BASE_URL are present and non-empty before
    frontend build

Security fixes included

  • requirePatientAccess tightened: missing/empty
    patientId now returns 403 (previously bypassed via
    falsy short-circuit)
  • Cross-tenant comment write: all entity IDs checked,
    not just the first
  • Upload IDOR: ownership check runs after multer
    parses multipart body
    Frontend
  • AuthProvider + useAuth() hook; ProtectedRoute; LoginPage with
    fetchOptions.onSuccess redirect (race-free, library-idiomatic)
  • All page queries gate on user.patientId; AppLayout logout button

Infra

  • infra/deploy.sh: build order fixed (full npm ci before tsc/prisma), migration
    hard-fails on error, restore evidence gate, 30s health poll
  • infra/systemd/havenhold-api.service: After=postgresql.service, EnvironmentFile from
    .env
  • deploy.sh validates VITE_AUTH_BASE_URL and VITE_API_BASE_URL are present and
    non-empty before frontend build

Security fixes included

  • requirePatientAccess tightened: missing/empty patientId now returns 403 (previously
    bypassed via falsy short-circuit)
  • Cross-tenant comment write: all entity IDs checked, not just the first
  • Upload IDOR: ownership check runs after multer parses multipart body

Test plan

  • npm run lint — 0 errors
  • cd server && npm run build — 0 errors
  • Unauthenticated request to /api/feed/:id returns 401
  • Refresh page → session persists
  • Logout → redirected to /login
  • Cross-patient request returns 403
  • curl smoke tests from runbook verification section

  session auth and add production deploy
Comment thread server/src/routes/documents.ts Dismissed
Comment thread server/src/routes/documents.ts Dismissed
Comment thread server/src/routes/documents.ts Dismissed
Comment thread server/src/routes/documents.ts Dismissed
Comment thread server/src/middleware/sessionAuth.ts Dismissed
Comment thread server/src/routes/documents.ts Dismissed
Comment thread server/src/routes/family.ts Dismissed
@dashprotocol dashprotocol merged commit 4b4a32d into main May 28, 2026
5 checks passed
@dashprotocol dashprotocol deleted the feat/H-010-real-auth branch May 28, 2026 09:42
@dashprotocol dashprotocol linked an issue May 28, 2026 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

H-010 Replace demo auth with real auth

2 participants