Improve rate-limit headers in auth API (#102)
Destructures `remaining` and `resetAt` from `checkRateLimit` and adds standard rate-limiting headers (`Retry-After`, `X-RateLimit-Limit`, `X-RateLimit-Remaining`) to the 429 Too Many Requests response. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
@@ -13,10 +13,20 @@ const AUTH_RPM = 10;
|
||||
export async function POST(request: NextRequest) {
|
||||
// ── Rate limiting (brute-force protection) ──────────
|
||||
const ip = getClientIp(request);
|
||||
const { success } = checkRateLimit(`auth:${ip}`, AUTH_RPM);
|
||||
const { success, remaining, resetAt } = checkRateLimit(`auth:${ip}`, AUTH_RPM);
|
||||
|
||||
if (!success) {
|
||||
return NextResponse.json({ error: 'Too many attempts, try again later' }, { status: 429 });
|
||||
return NextResponse.json(
|
||||
{ error: 'Too many attempts, try again later' },
|
||||
{
|
||||
status: 429,
|
||||
headers: {
|
||||
'Retry-After': String(Math.ceil((resetAt - Date.now()) / 1000)),
|
||||
'X-RateLimit-Limit': String(AUTH_RPM),
|
||||
'X-RateLimit-Remaining': String(remaining),
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user