feat: throttle admin login attempts

This commit is contained in:
i vib committed 2026-10-25 14:16:16 -07:00
1 parent 98d610271f
commit 31b310d1b9
2 files changed
+266 -6

No files matched your search

+35 -1
View File
@@ -1,5 +1,9 @@
import { createAdminSessionToken } from "@/core/auth/adminSession";
import { verifyAdminCredentials } from "@/core/auth/adminUsers";
import {
clearAdminLoginAttempts,
consumeAdminLoginAttempt,
verifyAdminCredentials,
} from "@/core/auth/adminUsers";
import { NextRequest, NextResponse } from "next/server";
import { createHash, timingSafeEqual } from "node:crypto";
@@ -37,6 +41,27 @@ export async function POST(request: NextRequest) {
);
}
let throttleDecision;
try {
throttleDecision = await consumeAdminLoginAttempt(email, sessionSecret);
} catch {
return NextResponse.json(
{ error: "Admin authentication is temporarily unavailable." },
{ status: 503 },
);
}
if (!throttleDecision.allowed) {
return NextResponse.json(
{ error: "Too many sign-in attempts. Try again later." },
{
status: 429,
headers: {
"Retry-After": String(throttleDecision.retryAfterSeconds),
},
},
);
}
let authenticated = false;
if (process.env.NODE_ENV !== "production" && !process.env.DATABASE_URL) {
const adminEmail = process.env.DATASTACK_ADMIN_EMAIL;
@@ -65,6 +90,15 @@ export async function POST(request: NextRequest) {
);
}
try {
await clearAdminLoginAttempts(email, sessionSecret);
} catch {
return NextResponse.json(
{ error: "Admin authentication is temporarily unavailable." },
{ status: 503 },
);
}
const remember = body.remember === true;
const maxAge = remember ? REMEMBERED_SESSION_SECONDS : SESSION_SECONDS;
const expiresAt = Date.now() + maxAge * 1000;