A3: auth (magic-link sign-in, requirePermission chokepoint, role enum)

- Auth.js v5 Credentials provider (magic-link) + JWT session carrying systemRole
- src/auth: roles, permissions (feature-scoped matrix), magic-link dev console transport
- requirePermission(user, perm) chokepoint -> AuthError(401/403)
- Routes: /api/auth/[...nextauth], /api/dev/magic-link, /api/dev/complete, /api/admin, /login
- Auto-activate new emails in dev (AUTO_ACTIVATE_NEW_USERS, flip to require approval)
- Acceptance pnpm test src/auth green: 401 anonymous, 403 volunteer on /admin, 200 admin
- Verified at runtime against Postgres on :5433
This commit is contained in:
Tiel Coder
2026-11-17 19:34:06 +00:00
parent a68e1c6277
commit 3cd932e1a1
16 changed files with 779 additions and 1 deletions
+36
View File
@@ -73,3 +73,39 @@ Build log for Media Team Hub. One unit per commit, top to bottom. Append after e
- `dateInTimezone` destructured `{y,m,d}` but `partsInTz` returns `{year,month,day}` — fixed.
**Next: A3 — auth (auth.js + credentials provider, admin/coordinator/volunteer roles).**
---
## A3 — Auth — DONE 2026-11-17
**Scope (from BUILD-PLAN.md):** magic-link sign-in (dev console transport), session cookie, `requirePermission()` chokepoint, role enum.
**Delivered:**
- `src/auth/roles.ts` — `SystemRole` type + helpers (`isAdmin`, `isCoordinator`, `isLeader`, `assertSystemRole`).
- `src/auth/permissions.ts` — feature-scoped permission matrix from the ARCHITECTURE §2.3 capability table, `hasPermission()`, `AuthError` (carries HTTP 401/403), `requirePermission(user, perm)` chokepoint.
- `src/auth/magic-link.ts` — dev console transport: `getOrCreateUser` (auto-activates unknown emails when `AUTO_ACTIVATE_NEW_USERS` is on — dev convenience; flip to `"false"` for the B1 approval flow), `signMagicToken`/`verifyMagicToken` (jose HS256), `requestMagicLink` (prints the sign-in link to the console).
- `src/auth/config.ts` — Auth.js v5 Credentials provider (`magic-link`) + JWT session carrying `systemRole`.
- Routes: `/api/auth/[...nextauth]`, `/api/dev/magic-link` (POST `{email}`), `/api/dev/complete` (GET `?token=…`), `/api/admin` (chokepoint), and `/login` (magic-link sign-in UI).
**Verification (all exit 0):**
- `pnpm typecheck` ✓ · `pnpm lint` ✓ · `pnpm test` ✓ (17 tests) · `pnpm build` ✓
- **Acceptance** `pnpm test src/auth` green: 401 anonymous, 403 volunteer on `/admin`, 200 admin ✓
- Runtime smoke test (dev server + Postgres on :5433):
- `GET /api/admin` (no session) → 401 `UNAUTHORIZED` ✓
- `POST /api/dev/magic-link {email}` → 200, creates user (volunteer/active), returns + logs the sign-in link ✓
- `GET /api/dev/complete?token=…` → 200, token verified, session established ✓
- User persisted in DB as `active` ✓
- `GET /login` → 200, renders ✓
**Decisions:**
- **Auto-activate new emails in dev** (`AUTO_ACTIVATE_NEW_USERS`, default on) so a volunteer can sign in as anyone without admin setup; admin can flip to `"false"` to require approval (B1).
- **Auth.js v5** (matches the docs' "Auth.js"; portable, no SaaS lock-in). JWT sessions, no DB adapter needed. (auth.js v5 ships as `next-auth@beta`; the `@auth/next` scope isn't published yet.)
- **`/admin` requires `admin.settings`** (admin-only) — matches the spec's `/admin/*` pages being admin-only.
**Bugs fixed this phase (recorded for future reference):**
- Auth.js v5 session config uses `session.strategy: "jwt"`, not `session.jwt`.
- `req.json()` was unreliable here; `req.text()` + `JSON.parse` is the robust body read.
- An object schema must be parsed as an object: `bodySchema.safeParse({ email })`, not `safeParse(email)`.
- Turbopack dev + a production `.next/` from `pnpm build` conflict on `_buildManifest.js.tmp`; `rm -rf .next` before `next dev`.
**Next: A4 — app shell (layout, nav, tokens, error + empty states, `/api/health`).