Implement auth middleware for API routes (P0.2)
Add authentication infrastructure for protected routes: - withAuth() wrapper for API route handlers (src/lib/auth-middleware.ts) - Next.js middleware for page protection (src/middleware.ts) withAuth() loads auth from cookies, validates session, and passes user context to handlers. Returns 401 for unauthenticated requests. Page middleware redirects unauthenticated users to /login, while allowing public routes (/login), API routes (handled separately), and static assets through. Tests: 18 new tests (6 for withAuth, 12 for page middleware) Total test count: 60 tests passing 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -15,10 +15,12 @@ This file is maintained by Ralph. Run `./ralph-sandbox.sh plan 3` to generate ta
|
||||
| `decision-engine.ts` | **COMPLETE** | 8 priority rules + override handling with `getDecisionWithOverrides()`, 24 tests |
|
||||
| `garmin.ts` | **Minimal (~30%)** | Has fetchGarminData, isTokenExpired, daysUntilExpiry. **MISSING: fetchHrvStatus, fetchBodyBattery, fetchIntensityMinutes** |
|
||||
| `pocketbase.ts` | **COMPLETE** | 9 tests covering `createPocketBaseClient()`, `isAuthenticated()`, `getCurrentUser()`, `loadAuthFromCookies()` |
|
||||
| `auth-middleware.ts` | **COMPLETE** | 6 tests covering `withAuth()` wrapper for API route protection |
|
||||
| `middleware.ts` (Next.js) | **COMPLETE** | 12 tests covering page protection, redirects to login |
|
||||
|
||||
### Missing Infrastructure Files (CONFIRMED NOT EXIST)
|
||||
- `src/lib/auth-middleware.ts` - Does NOT exist, needs creation
|
||||
- `src/app/middleware.ts` - Does NOT exist, needs creation
|
||||
- ~~`src/lib/auth-middleware.ts`~~ - **CREATED** in P0.2
|
||||
- ~~`src/middleware.ts`~~ - **CREATED** in P0.2
|
||||
|
||||
### API Routes (12 total)
|
||||
| Route | Status | Notes |
|
||||
@@ -66,6 +68,8 @@ This file is maintained by Ralph. Run `./ralph-sandbox.sh plan 3` to generate ta
|
||||
| `src/lib/cycle.test.ts` | **EXISTS** - 9 tests |
|
||||
| `src/lib/decision-engine.test.ts` | **EXISTS** - 24 tests (8 algorithmic rules + 16 override scenarios) |
|
||||
| `src/lib/pocketbase.test.ts` | **EXISTS** - 9 tests (auth helpers, cookie loading) |
|
||||
| `src/lib/auth-middleware.test.ts` | **EXISTS** - 6 tests (withAuth wrapper, error handling) |
|
||||
| `src/middleware.test.ts` | **EXISTS** - 12 tests (page protection, public routes, static assets) |
|
||||
| `src/lib/nutrition.test.ts` | **MISSING** |
|
||||
| `src/lib/email.test.ts` | **MISSING** |
|
||||
| `src/lib/ics.test.ts` | **MISSING** |
|
||||
@@ -96,13 +100,14 @@ These must be completed first - nothing else works without them.
|
||||
- **Why:** Every protected route and page depends on these helpers
|
||||
- **Blocking:** P0.2, P0.4, P1.1-P1.7, P2.2-P2.13
|
||||
|
||||
### P0.2: Auth Middleware for API Routes
|
||||
- [ ] Create reusable auth middleware for protected API endpoints
|
||||
### P0.2: Auth Middleware for API Routes ✅ COMPLETE
|
||||
- [x] Create reusable auth middleware for protected API endpoints
|
||||
- **Files:**
|
||||
- `src/lib/auth-middleware.ts` - **CREATE** `withAuth()` wrapper for route handlers
|
||||
- `src/app/middleware.ts` - **CREATE** Next.js middleware for page protection
|
||||
- `src/lib/auth-middleware.ts` - Added `withAuth()` wrapper for route handlers
|
||||
- `src/middleware.ts` - Added Next.js middleware for page protection
|
||||
- **Tests:**
|
||||
- `src/lib/auth-middleware.test.ts` - Test unauthorized rejection, user context passing
|
||||
- `src/lib/auth-middleware.test.ts` - 6 tests covering unauthorized rejection, user context passing, error handling
|
||||
- `src/middleware.test.ts` - 12 tests covering protected routes, public routes, API routes, static assets
|
||||
- **Why:** All API routes except `/api/calendar/[userId]/[token].ics` and `/api/cron/*` require auth
|
||||
- **Depends On:** P0.1
|
||||
- **Blocking:** P0.4, P1.1-P1.5
|
||||
@@ -477,8 +482,8 @@ P2.14 Mini calendar
|
||||
|
||||
*Bugs and inconsistencies found during implementation*
|
||||
|
||||
- [ ] `src/lib/auth-middleware.ts` does not exist - must be created in P0.2
|
||||
- [ ] `src/app/middleware.ts` does not exist - must be created in P0.2
|
||||
- [x] ~~`src/lib/auth-middleware.ts` does not exist~~ - CREATED in P0.2
|
||||
- [x] ~~`src/middleware.ts` does not exist~~ - CREATED in P0.2
|
||||
- [ ] `garmin.ts` is only ~30% complete - missing specific biometric fetchers
|
||||
- [x] ~~`pocketbase.ts` missing all auth helper functions~~ - FIXED in P0.1
|
||||
|
||||
|
||||
Reference in New Issue
Block a user