refactor: extract admin auth gate into requireAdmin() helper#161
refactor: extract admin auth gate into requireAdmin() helper#161cleanjunc wants to merge 1 commit into
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (6)
📝 WalkthroughWalkthroughThis PR extracts repeated admin authorization logic from five separate API routes into a centralized ChangesAdmin Auth Gate Extraction
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
src/app/api/admin/pending-count/route.tsESLint skipped: missing config or dependency (missing-dependency). The ESLint configuration references a package that is not available in the sandbox. src/app/api/admin/users/[id]/approve/route.tsESLint skipped: the ESLint configuration for this file references a package that is not available in the sandbox. src/app/api/admin/users/[id]/promote/route.tsESLint skipped: the ESLint configuration for this file references a package that is not available in the sandbox.
Warning Review ran into problems🔥 ProblemsStopped waiting for pipeline failures after 30000ms. One of your pipelines takes longer than our 30000ms fetch window to run, so review may not consider pipeline-failure results for inline comments if any failures occurred after the fetch window. Increase the timeout if you want to wait longer or run a 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 |
Summary
The same session check,
is_admincheck, and 401/403 response pair was duplicated across five admin API routes. This PR pulls that gate into a singlerequireAdmin()helper insrc/lib/auth.tsand updates the affected routes to call it.The helper returns
{ session, user }on success and aNextResponse(401 or 403) on failure, which slots cleanly into the existing early return pattern at each call site:Routes migrated:
src/app/api/admin/users/route.tssrc/app/api/admin/users/[id]/approve/route.tssrc/app/api/admin/users/[id]/promote/route.tssrc/app/api/admin/users/[id]/reject/route.tssrc/app/api/admin/pending-count/route.tsNo behaviour change: status codes, response bodies, and the actor passed to
approveUser/promoteUser/rejectUserall match the originals.Related Issues
Closes #160
Type of Change
Testing
pnpm buildpassesAlso verified with
npx tsc --noEmitandeslinton the touched files. Admin endpoints still return 401 for unauthenticated callers, 403 for non-admins, and 200 for admins, matching pre-refactor behaviour.Checklist
Summary by CodeRabbit