Move cookie-only API routes onto authedRoute [ABD-21]
Merged
FR
6d ago+803+803+012345678901234567890123456789-765-765-012345678901234567890123456789
What & why
Second step of ABD-21. Stacked on #720 (base: abd-21-single-request-principal-one-scoped-db-c); merge that first.
#720 added the authedRoute wrapper for the six token/extension routes. This moves the 39 cookie-only API route files (71 handlers) onto it too, so they no longer repeat createClient() → getCompletedSignupUser() → 401 by hand.
credentials: "session"— a newauthenticateRequestoption. TheAuthorizationheader is never read, so neither personal access tokens nor the extension's Supabase bearer tokens reach these routes. Who can call each route is unchanged; opening one to bearer tokens stays a per-route decision.allowAnonymous: true— a new wrapper option: a signed-out request reaches the handler with anullprincipal instead of a 401. Used by the five handlers that already served visitors:images/[fileKey],items/[id]/pages,rooms/[id]/itemsGET,map-image,twitter-video.- Admin routes (
admin/waitlist/invite,items/[id]/retry,items/[id]/reassign) keep their admin check inside the handler. token-route-access.test.tsreadscredentials: "session"as well. Session-only routes stay off the reviewed list, so a route switching to atokenScopeappears there and fails.- Route tests mock
authenticateRequestwith their existing user mocks. The profile route's incomplete-signup case is dropped there:authenticateRequest's own tests cover it on the cookie path. - Agent guidance (
app/src/app/AGENTS.md,add-authed-pageskill, rootAGENTS.md) now points API routes atauthedRoute({ credentials: "session" }).
Thread: https://ampcode.com/threads/T-01a1e700-7a74-76b5-85a8-6c3a6faaad6c
Checklist
- User-facing change → added/updated a PostHog event (and a funnel/insight for a new flow), or N/A — N/A, internal refactor with no new user action
- User-facing or service change → docs updated (README features/roadmap/services,
.env.example, in-app help,CONTRIBUTING.md, onboarding checklist), or N/A — N/A for user docs; agent guidance updated as above - New error paths report via
captureServerException/ the error boundary, or N/A — N/A, no new error paths (auth failures go through #720's wrapper) - New behavior is covered by tests, or N/A — cookie-only routes ignoring both bearer token kinds (
authenticate-request.test.ts),allowAnonymous(authed-route.test.ts); existing route tests run through the real wrapper - User-facing change → demo video attached (
record-pr-demoskill); refactor through a user flow → parity run, or N/A — N/A, API auth plumbing only; route tests, the cross-user isolation suite and E2E cover behaviour - If this fixes a recurring defect, considered a guardrail via the
learnskill, or N/A — N/A, not a defect fix
Notes
Verified locally:
bun run check:fixclean; full unit suite passes (4,345 tests). The one remaining lint warning (useOptionalChaininrooms/[id]/sync/[runId]) is already onmain.- The new cookie-only tests fail when session routes are changed to read the bearer header again.
token-route-access.test.tsfails when a session-only route (items/status) is switched totokenScope: "read".cross-user-isolation.integration.test.ts: 260/260 pass (with placeholder Supabase env values).- Replayed Next's generated route-signature check (the one that failed the Build on #720) against all 71 wrapped handlers: passes.
Not done here:
admin/users,admin/users/[id]andadmin/statsnever usedgetCompletedSignupUserand still usewithViewer; they need handling before the next step.- Next step: a test requiring every API route export to go through a wrapper, replacing
no-api-incomplete-signup.grit.
Summary by cubic
Moves the 39 cookie-only API route files (71 handlers) onto authedRoute({ credentials: "session" }), replacing the repeated createClient() → getCompletedSignupUser() → 401 boilerplate with a wrapper the routes share.
credentials: "session"is a newauthenticateRequestoption; the Authorization header is never read, so neither personal access tokens nor the extension's Supabase bearer tokens reach these routes.allowAnonymous: trueruns a signed-out request's handler with a null principal instead of a 401; the five handlers that already served visitors use it.- Admin routes keep their admin check in the handler; route tests mock
authenticateRequestwith their existing user mocks, and the profile route's incomplete-signup case moved toauthenticate-request.test.ts. - The wrapper's 500 path no longer parses
request.urlwhen building its error label, so an auth failure with an invalid-URL request answers 500 instead of crashing. - Agent guidance now points API routes at
authedRoute({ credentials: "session" }).