feat(api): withAuth() wrapper + ok/fail envelopes (Phase 1)#14
Merged
Conversation
~40 routes hand-roll the same getUser()→401 check with three stylistic
variants and drifting response envelopes. Add a single seam:
- withAuth(handler): resolves the user, returns a 401 JSON envelope on failure
(never a redirect — fetch clients get a usable error), and invokes the handler
with { request, user, supabase, params }. Generic over Next 16's
params: Promise<P>.
- ok(data) → { data }; fail(error, status, extra?) → { error, ...extra }.
- 7 unit tests (401 paths, handler invocation, envelope shapes).
Migrates two routes as proof the pattern preserves response shapes exactly
(distill/run, signals/scan). Remaining routes migrate incrementally in
follow-ups.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
~40 API routes hand-roll the same
getUser() → 401check, in three stylistic variants, with drifting response envelopes ({data}vs bare vs{error}). The audit flagged this as a root cause of route-level inconsistency. Some routes also rely on middleware, which redirects unauthenticated/api/*calls to/login(a 307) — wrong for fetch clients that expect JSON.What
A single auth seam in
src/lib/api/withAuth.ts:withAuth(handler)— resolves the Supabase user, returns a 401 JSON envelope on failure (never a redirect), and invokes the handler with{ request, user, supabase, params }. Generic over Next 16'sparams: Promise<P>.ok(data)→{ data }(200 default);fail(error, status, extra?)→{ error, ...extra }.Scope
This PR lands the foundation plus two proof migrations (
distill/run,signals/scan) that preserve response shapes byte-for-byte ({data: result},{error}401/500). The remaining ~38 routes migrate incrementally in follow-up PRs to keep each diff reviewable — a single 40-file auth rewrite is exactly where subtle per-route regressions hide.A separate follow-up will switch middleware to return 401 JSON for
/api/*(instead of the login redirect) so the wrapper's 401 is actually reached.Test plan
vitest run→ 545 pass (incl. withAuth 7 + distill route tests under the new export style)tsc --noEmit→ 0 errorseslint .→ 0 errorsPart of Phase 1. Sibling PR #13 (cost-table fix) is independent. Structured-outputs migration is the next focused PR; Supabase types + migrations-dir are deferred pending the Supabase CLI.