# A4 Handoff — App Shell **Status:** In progress. A1, A2, A3 committed. A4 work is uncommitted. **Resolved:** The `/api/dev/complete` session bug (see below). With it fixed, all 4 e2e shell tests pass, plus typecheck/lint/17 unit tests. Next session: commit A4 (instructions at the bottom of this file). --- ## What's Done - **A1** (commit `9cdf8be`): Foundation scaffold — Next.js 15, shadcn/Radix, Drizzle+Postgres, Vitest, Docker. - **A2** (commit `d133aff`): Schema core — 6 tables, migration, idempotent seed. - **A3** (commit `c0e73e2`): Auth — magic-link sign-in, `requirePermission()` chokepoint, role enum. ## A4 Work (Uncommitted) Built for the app shell: - `src/components/app/nav.tsx` — Nav (5 tabs mobile / rail desktop). Uses lucide-react icons. - `src/components/app/state-message.tsx` — Error + empty states. - `src/components/app/app-layout.tsx` — Shell layout (header, nav, main, footer). - `src/app/(app)/layout.tsx` — Authenticated route group (redirects to /login if no session). - `src/app/(app)/schedule/page.tsx` — `src/app/(app)/roster/page.tsx` — `src/app/(app)/training/page.tsx` — `src/app/(app)/announcements/page.tsx` — `src/app/(app)/settings/page.tsx` — 5 shell pages. - `src/app/api/health/route.ts` — Health check endpoint. - `e2e/shell.spec.ts` — Playwright e2e test (5 nav tabs at 360px viewport). - `playwright.config.ts` — Playwright config. - `package.json` — Added `test:e2e` script. - `src/app/api/dev/complete/route.ts` — Added try/catch + logging around `signIn`. --- ## The Blocker (RESOLVED): `/api/dev/complete` never set a session cookie **Original report:** `GET /api/dev/complete?token=...` appeared to return HTTP 000 / connection reset. That was a **test-harness artifact** (curl URL parsing/quoting in the handoff's shell script), not an app bug — the endpoint returns `200 {"ok":true}`. **Real symptom:** The complete response carried **no `Set-Cookie` header**, so no session cookie was established. Every subsequent navigation to `/schedule` (etc.) then 307-redirected to `/login`, and the two shell e2e tests failed (nav tabs never rendered). The route reported `{"ok":true}` because `signIn` returned the error-page URL rather than throwing. **Root cause:** `[auth][error] UntrustedHost: Host must be trusted.` - Auth.js v5's `assertConfig()` checks `config.trustHost` **before** `setEnvDefaults()` applies its env-based default (the code comment says so explicitly). - `setEnvDefaults()` only derives `trustHost` from `AUTH_URL` / `AUTH_TRUST_HOST` / `VERCEL` / `CF_PAGES` / `NODE_ENV !== "production"` — **not** from `NEXTAUTH_URL`. - In `pnpm start` (production, `NODE_ENV=production`) with no `AUTH_URL`, `trustHost` stayed `false`, so the magic-link callback was rejected and no session cookie was written. - `pnpm dev` worked (dev mode) and Docker set `NEXTAUTH_URL` — but that var does not set `trustHost`, so Docker had the same latent bug. **Fix:** `src/auth/config.ts` now sets `trustHost: true` explicitly (the documented fix for `UntrustedHost`; this is a localhost app behind a reverse proxy). Rebuild required for `pnpm start`. Verified: complete response now sets `authjs.session-token`, `/schedule` returns 200, and all 4 e2e shell tests pass. --- ## How to Test (for next session) Start the app and test the complete flow: ```bash cd /home/bhursey/code/glenlochmedia # Start app (detached, kills itself on exit) env DATABASE_URL="postgres://media:media@localhost:5433/media" \ NEXTAUTH_SECRET="e2e-secret-for-shell-tests" \ AUTO_ACTIVATE_NEW_USERS="true" \ PORT=3000 \ pnpm start >/tmp/app.log 2>&1 & APP_PID=$! # Wait for ready for i in $(seq 1 30); do grep -qiE "Ready" /tmp/app.log && break; sleep 1; done # Get a token TOKEN_URL=$(curl -s -X POST localhost:3000/api/dev/magic-link \ -H 'Content-Type: application/json' \ -d '{"email":"e2e-test@example.com"}' \ | python3 -c "import sys,json; print(json.load(sys.stdin)['url'])") # Complete sign-in (this is where HTTP 000 happens) curl -sv "http://localhost:3000$TOKEN_URL" 2>&1 | grep -iE "set-cookie|HTTP/|error" # Cleanup kill $APP_PID ``` **Note:** Use valid emails (with a TLD). Zod's `.email()` rejects `@localhost`. --- ## E2E Test ```bash cd /home/bhursey/code/glenlochmedia pnpm test:e2e --grep shell ``` The webServer runs `pnpm db:migrate && pnpm build && pnpm start` on port 3000. **Current e2e status:** Both shell tests fail because `signInAs` doesn't establish a session (the complete endpoint resets the connection). Once the complete endpoint works, the e2e tests should pass. --- ## Known Good Commands - Typecheck: `pnpm typecheck` (exit 0) - Lint: `pnpm lint` (exit 0) - Unit tests: `pnpm test` (17 tests, exit 0) - Build: `pnpm build` (exit 0) - DB migrate: `pnpm db:migrate` (needs Postgres on :5433) - DB seed: `pnpm db:seed` --- ## To Commit A4 (once complete) ```bash cd /home/bhursey/code/glenlochmedia rm -f src/app/api/dev/magic-debug.log # debug file, don't commit git add -A git commit -m "A4: app shell (layout, nav, tokens, error/empty states, /api/health)" ```