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:
Ralksta
2026-04-21 22:51:42 +02:00
committed by GitHub
parent 3bbec5a4ac
commit bb7c1aa1bd

View File

@@ -13,10 +13,20 @@ const AUTH_RPM = 10;
export async function POST(request: NextRequest) { export async function POST(request: NextRequest) {
// ── Rate limiting (brute-force protection) ────────── // ── Rate limiting (brute-force protection) ──────────
const ip = getClientIp(request); const ip = getClientIp(request);
const { success } = checkRateLimit(`auth:${ip}`, AUTH_RPM); const { success, remaining, resetAt } = checkRateLimit(`auth:${ip}`, AUTH_RPM);
if (!success) { 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 { try {