Skip to content

feat(api): withAuth() wrapper + ok/fail envelopes (Phase 1)#14

Merged
gurdenbatra merged 1 commit into
mainfrom
phase-1-withauth
Jun 12, 2026
Merged

feat(api): withAuth() wrapper + ok/fail envelopes (Phase 1)#14
gurdenbatra merged 1 commit into
mainfrom
phase-1-withauth

Conversation

@gurdenbatra

Copy link
Copy Markdown
Member

Why

~40 API routes hand-roll the same getUser() → 401 check, 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's params: Promise<P>.
  • ok(data){ data } (200 default); fail(error, status, extra?){ error, ...extra }.
  • 7 unit tests: both 401 paths, handler invocation with the right context, and envelope shapes.

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)
  • clean tsc --noEmit → 0 errors
  • eslint . → 0 errors

Part 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.

~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.
@vercel

vercel Bot commented Jun 11, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
cof-learning-system Ready Ready Preview, Comment Jun 11, 2026 10:10pm

Request Review

@gurdenbatra gurdenbatra merged commit 125da41 into main Jun 12, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant