Add authedRoute wrapper and move token routes onto it [ABD-21]
What & why
First step of ABD-21 (one request principal + one scoped DB client). Each API route used to authenticate by hand: open a viewer frame, call authenticateRequest with a scope, return a 401, and add CORS headers. This adds an authedRoute wrapper that does all of that, so a route only declares what it accepts:
export const POST = authedRoute({ tokenScope: "write", cors: true }, handlePOST);
AuthenticatedRequestis renamed toPrincipal: who the request acts for. OnlyauthenticateRequestbuilds one, so holding one means the 2FA, completed-signup and token-scope checks passed.authedRoute(src/lib/http/authed-route.ts) opens the viewer frame, authenticates with the declaredtokenScope, answers 401 (or 500 if the lookup throws) before the handler runs, optionally adds CORS headers (the 401 included), and passes the handler itsparamsplus theprincipal.- The six routes that called
authenticateRequestnow use it:mcp,imports/literal,imports/[id],items/from-url,items/notes,items/[id]/instagram-enrich. token-route-access.test.tsnow reads each scope from theauthedRoute(...)call and fails if anything other than the wrapper callsauthenticateRequest.
Behaviour differences: every wrapped route's 401 now carries WWW-Authenticate: Bearer realm="abode" (before, only MCP did), and an auth-lookup failure is reported under the request's method and path rather than a per-route label.
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 (no user-facing or service change); agent guidance (AGENTS.md,app/src/app/AGENTS.md,add-authed-pageskill) now points token routes atauthedRoute - New error paths report via
captureServerException/ the error boundary, or N/A — the wrapper's auth-failure 500 reports viacaptureServerException - New behavior is covered by tests, or N/A —
authed-route.test.tscovers the wrapper's contract; 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 and the cross-user isolation suite cover behaviour - If this fixes a recurring defect, considered a guardrail via the
learnskill, or N/A — N/A, not a defect fix; the token-scope guardrail is kept and moved onto the wrapper
Notes
Verified locally:
bun run check:fixclean; full unit suite passes (4,303 tests).- New
authed-route.test.ts: 401/500 without running the handler, the declared scope reachingauthenticateRequest, params + principal inside a viewer frame, CORS on both the 401 and success responses. token-route-access.test.tsfails when a route's scope is changed (checked by temporarily switchingitems/notesfromwritetoread).cross-user-isolation.integration.test.ts: 260/260 pass, including the viewer-frame check and the owner-with-RLS cases (run with placeholder Supabase env values).
Not done here (later ABD-21 steps): cookie-only routes still use getCompletedSignupUser / getUserWithMfa; pages, server actions, asSystem and turning RLS on are still to come.
Summary by cubic
First step of ABD-21 (one request principal + one scoped DB client). API routes used to authenticate by hand: open a viewer frame, call authenticateRequest with a scope, return a 401, and add CORS headers. Adds an authedRoute wrapper that does all of that, so a route only declares what it accepts on its export. Renames AuthenticatedRequest to Principal.
- The six routes that previously called
authenticateRequestby hand now declare their scope on the export:mcp,imports/literal,imports/[id],items/from-url,items/notes, anditems/[id]/instagram-enrich. authedRoutereturns 401 (or 500 if the auth lookup throws) before running the handler, optionally adds CORS headers to the 401 and the response, and passes the handler itsparamsplus theprincipal.tokenScopeonly governs personal access tokens; cookie sessions and Supabase bearer tokens (the extension) authenticate regardless, and it must be a literal so the access test can read it.- Handlers without route params get a one-argument signature so Next's generated route types accept them.
- Every wrapped route's 401 now carries
WWW-Authenticate: Bearer realm="abode"; before, onlymcpdid. - Auth-lookup failures are reported under the request's method and path rather than a per-route label.
token-route-access.test.tsreads each scope from theauthedRoute(...)call and fails if anything other than the wrapper callsauthenticateRequest.- Auth guidance in
AGENTS.mdand theadd-authed-pageskill now points token routes atauthedRoute, noting the literal scope, the 500 path, and thattokenScopegoverns PATs only.