-
Notifications
You must be signed in to change notification settings - Fork 0
feat(api): align response contract with shared packages #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughReplaces environment access with a validated, cached env module; adds Supabase-based auth client and replaces header-based authentication in the API handler; introduces three POST auth endpoints (login, signup, token) with Zod validation and centralized route utilities; updates config to load env before Next.js config and bumps related dependencies. Changes
Sequence Diagram(s)sequenceDiagram
autonumber
participant Client
participant Route as Auth Route
participant Utils as auth/utils.ts
participant Validation as auth/validation.ts
participant Supabase as Supabase Auth Client
participant Response
Client->>Route: POST /api/auth/login {email,password}
Route->>Utils: parseJsonBody(request, loginSchema)
Utils->>Validation: safeParse(loginSchema)
alt valid
Validation-->>Utils: parsed data
Utils-->>Route: parsed input
Route->>Supabase: login(parsed input)
alt success
Supabase-->>Route: token data
Route->>Utils: respondWithData(token,200)
Utils-->>Response: 200 JSON
else auth error
Supabase-->>Route: SupabaseAuthError
Route->>Route: convert to ApiError
Route->>Utils: handleRoute -> respondWithError
Utils-->>Response: error JSON (status)
end
else invalid
Validation-->>Utils: first issue message
Utils->>Response: 400 JSON error
end
Response-->>Client: HTTP response
sequenceDiagram
autonumber
participant App as Next.js
participant Env as src/app/env.ts
participant Cache as cachedEnv
participant Handler as api/handler.ts
App->>Handler: initialize/request
Handler->>Env: getEnv()
alt cache empty
Env->>Env: buildEnv() / zod validate
Env-->>Cache: store validated AppEnv
Cache-->>Handler: AppEnv
else cached
Cache-->>Handler: AppEnv
end
Handler->>Handler: getAuthentication() (lazy) -> cached provider
Handler->>Supabase: provider.authenticate(context)
Handler->>App: proceed with request dispatch
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes
Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🔇 Additional comments (2)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
bun.lockis excluded by!**/*.lock
📒 Files selected for processing (12)
README.md(1 hunks)drizzle.config.ts(1 hunks)next.config.ts(1 hunks)package.json(1 hunks)src/app/api/auth/login/route.ts(1 hunks)src/app/api/auth/signup/route.ts(1 hunks)src/app/api/auth/token/route.ts(1 hunks)src/app/api/auth/utils.ts(1 hunks)src/app/api/auth/validation.ts(1 hunks)src/app/api/handler.ts(2 hunks)src/app/env.ts(1 hunks)src/app/supabase-auth-client.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (6)
src/app/supabase-auth-client.ts (1)
src/app/env.ts (1)
getEnv(42-47)
src/app/api/auth/signup/route.ts (5)
src/app/api/auth/login/route.ts (1)
POST(12-26)src/app/api/auth/token/route.ts (1)
POST(12-26)src/app/api/auth/utils.ts (4)
handleRoute(41-53)parseJsonBody(12-31)ApiError(3-10)respondWithData(33-35)src/app/api/auth/validation.ts (1)
signupSchema(25-31)src/app/supabase-auth-client.ts (1)
getSupabaseAuthClient(9-20)
drizzle.config.ts (1)
src/app/env.ts (1)
getEnv(42-47)
src/app/api/auth/token/route.ts (5)
src/app/api/auth/login/route.ts (1)
POST(12-26)src/app/api/auth/signup/route.ts (1)
POST(12-26)src/app/api/auth/utils.ts (4)
handleRoute(41-53)parseJsonBody(12-31)respondWithData(33-35)ApiError(3-10)src/app/api/auth/validation.ts (1)
tokenSchema(33-35)src/app/supabase-auth-client.ts (1)
getSupabaseAuthClient(9-20)
src/app/api/auth/login/route.ts (5)
src/app/api/auth/signup/route.ts (1)
POST(12-26)src/app/api/auth/token/route.ts (1)
POST(12-26)src/app/api/auth/utils.ts (4)
handleRoute(41-53)parseJsonBody(12-31)respondWithData(33-35)ApiError(3-10)src/app/api/auth/validation.ts (1)
loginSchema(20-23)src/app/supabase-auth-client.ts (1)
getSupabaseAuthClient(9-20)
src/app/api/handler.ts (1)
src/app/env.ts (1)
getEnv(42-47)
🪛 markdownlint-cli2 (0.18.1)
README.md
32-32: Tables should be surrounded by blank lines
(MD058, blanks-around-tables)
🔇 Additional comments (11)
README.md (1)
1-73: Comprehensive documentation updates look good!The README now provides clear guidance on architecture, environment configuration, API surface, and local development workflows. The API table is well-structured and the authentication flow is accurately described.
package.json (1)
15-24: Dependency updates align with PR objectives.The addition of @t3-oss/env-nextjs, updated Zod to v4.1.12, and upgraded shared @Listee packages enable the centralized environment validation and Supabase auth integration. The versions appear current and compatible with Next.js 15 and React 19.
src/app/api/auth/utils.ts (1)
3-53: Well-structured API utilities with proper error handling.The utilities provide a clean abstraction for request validation, response formatting, and error handling:
ApiErrorclass correctly extends Error with a status codeparseJsonBodysafely validates requests using Zod with appropriate error messages- Response helpers maintain the documented contract (
{ data }for success,{ error }for failures)handleRouteprevents error detail leakage to clients while logging unexpected errors server-sidesrc/app/api/auth/token/route.ts (1)
12-26: Token refresh route correctly implements the authentication pattern.The POST handler follows the same structure as login/signup routes with proper input validation, error translation, and standardized responses. The use of
handleRouteensures consistent error handling across all auth endpoints.src/app/api/auth/signup/route.ts (1)
12-26: Signup route follows the established authentication pattern.The implementation correctly validates input, handles Supabase errors, and returns a null data response on success (appropriate for signup operations that may require email confirmation before token issuance).
next.config.ts (1)
1-20: Async config correctly loads environment before Next.js starts.The implementation uses jiti to dynamically import and validate the environment module before returning the Next.js configuration. This ensures environment variables are validated early in the startup process, following the fail-fast principle. The async config pattern is fully supported in Next.js 15.
src/app/supabase-auth-client.ts (1)
9-20: Singleton pattern with lazy initialization is appropriate here.The cached client factory correctly initializes the Supabase auth client on first access using validated environment variables. While there's a theoretical race condition in concurrent initialization, it's benign for this use case since
createSupabaseAuthClientappears to be a lightweight config wrapper and multiple instances would be functionally equivalent.src/app/api/auth/login/route.ts (1)
12-26: Login route completes the consistent authentication trio.The implementation follows the exact same pattern as signup and token routes, with proper validation, error translation, and standardized responses. The consistency across all three auth endpoints (
/login,/signup,/token) makes the codebase maintainable and predictable.drizzle.config.ts (1)
9-26: Nice defensive env validation upgrade.
WrappinggetEnv()in the try/catch and translating POSTGRES_URL failures into actionable messages will make Drizzle CLI errors far easier to diagnose.src/app/api/auth/validation.ts (1)
3-35: Reusable trim helper pays off.
trimmedNonEmptykeeps the auth schemas DRY while guarding against sneaky whitespace—nice touch.src/app/api/handler.ts (1)
78-107: Appreciate the cached auth + guarded dispatch.
Lazily memoizing the Supabase auth and standardizing the 500 fallback should make the route more resilient.
Summary
Testing
Summary by CodeRabbit
New Features
Refactor
Documentation
Chores