Files
maternal-app/maternal-web/app/api/auth/register/route.ts
Andrei 8e3567e3d6
Some checks failed
CI/CD Pipeline / Lint and Test (push) Has been cancelled
CI/CD Pipeline / E2E Tests (push) Has been cancelled
CI/CD Pipeline / Build Application (push) Has been cancelled
Add rate limiting to API endpoints
Implemented comprehensive rate limiting for API security:

- Created custom Next.js-native rate limiter using in-memory store
- Added 5 rate limit configurations:
  - authLimiter: 5 requests/15min for login/register/password-reset
  - aiLimiter: 10 requests/hour for AI assistant queries
  - trackingLimiter: 30 requests/min for activity tracking
  - readLimiter: 100 requests/min for read-only endpoints
  - sensitiveLimiter: 3 requests/hour for sensitive operations

- Applied rate limiting to endpoints:
  - /api/auth/login, /api/auth/register, /api/auth/password-reset
  - /api/ai/chat
  - /api/tracking/feeding (GET and POST)

- Rate limit responses include standard headers:
  - RateLimit-Limit, RateLimit-Remaining, RateLimit-Reset
  - Retry-After header with seconds until reset

- Tested with 7 sequential requests - first 5 passed, last 2 blocked with 429

Note: Current implementation uses in-memory store. For production with
multiple instances, migrate to Redis-backed storage for distributed
rate limiting.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-01 20:08:28 +00:00

48 lines
1.4 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
import { authLimiter } from '@/lib/middleware/rateLimiter';
/**
* Registration endpoint with rate limiting
* Limited to 5 attempts per 15 minutes per IP
*/
export async function POST(request: NextRequest) {
// Apply rate limiting
const rateLimitResult = await authLimiter(request);
if (rateLimitResult) return rateLimitResult;
try {
const body = await request.json();
const { email, password, name } = body;
// TODO: Implement actual registration logic
// This is a placeholder - actual registration will be handled by backend
// For now, forward to backend API
const backendUrl = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3020';
const response = await fetch(`${backendUrl}/api/v1/auth/register`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email, password, name }),
});
const data = await response.json();
if (!response.ok) {
return NextResponse.json(data, { status: response.status });
}
return NextResponse.json(data, { status: 201 });
} catch (error) {
console.error('[Auth] Registration error:', error);
return NextResponse.json(
{
error: 'AUTH_REGISTRATION_FAILED',
message: 'Registration failed. Please try again.',
},
{ status: 500 }
);
}
}