Files
DataStack/app/api/auth/login/route.ts
T

171 lines
4.4 KiB
TypeScript
Raw Normal View History

import {
type AdminRole,
createAdminSessionToken,
getAdminSessionSecret,
getSessionCookieOptions,
} from "@/core/auth/adminSession";
2026-10-25 14:16:16 -07:00
import {
AdminSetupRequiredError,
authenticateAdminCredentials,
2026-10-25 14:16:16 -07:00
clearAdminLoginAttempts,
consumeAdminLoginAttempt,
} from "@/core/auth/adminUsers";
import { SettingsManager } from "@/core/adminSettings/settingsManager";
import { recordAdminAuditLogAsync } from "@/core/auth/auditLog";
import { extractClientIp } from "@/lib/adminAuth";
2026-10-23 19:56:38 -07:00
import { NextRequest, NextResponse } from "next/server";
const SESSION_COOKIE = "datastack_admin_session";
const SESSION_SECONDS = 8 * 60 * 60;
const REMEMBERED_SESSION_SECONDS = 30 * 24 * 60 * 60;
export async function POST(request: NextRequest) {
const sessionSecret = getAdminSessionSecret();
2026-10-23 19:56:38 -07:00
2026-10-25 06:12:47 -07:00
if (!sessionSecret || sessionSecret.length < 32) {
2026-10-23 19:56:38 -07:00
return NextResponse.json(
{
2026-10-25 06:12:47 -07:00
error: "Admin authentication is not configured.",
2026-10-23 19:56:38 -07:00
},
{ status: 503 },
);
}
let body: Record<string, unknown>;
2026-10-23 19:56:38 -07:00
try {
const parsed = await request.json();
body =
parsed && typeof parsed === "object" && !Array.isArray(parsed)
? (parsed as Record<string, unknown>)
: {};
2026-10-23 19:56:38 -07:00
} catch {
return NextResponse.json(
{ error: "Invalid login request." },
{ status: 400 },
);
}
const email =
typeof body.email === "string" ? body.email.trim().toLowerCase() : "";
const password = typeof body.password === "string" ? body.password : "";
if (!email || password.length === 0 || password.length > 1024) {
return NextResponse.json(
{ error: "Invalid email or password." },
{ status: 401 },
);
}
const clientIp = extractClientIp(request);
let throttleDecision;
try {
throttleDecision = await consumeAdminLoginAttempt(
email,
sessionSecret,
clientIp,
);
} catch {
return NextResponse.json(
{ error: "Admin authentication is temporarily unavailable." },
{ status: 503 },
);
}
if (!throttleDecision.allowed) {
SettingsManager.triggerWebhookAlert({
event: "ADMIN_LOGIN_LOCKOUT",
severity: "warning",
title: "Admin Login Lockout Triggered",
message: `Admin sign-in throttled for ${email} from IP ${clientIp} (${throttleDecision.retryAfterSeconds}s lockout).`,
details: {
email,
clientIp,
retryAfterSeconds: throttleDecision.retryAfterSeconds,
},
});
return NextResponse.json(
{ error: "Too many sign-in attempts. Try again later." },
{
status: 429,
headers: {
"Retry-After": String(throttleDecision.retryAfterSeconds),
},
},
);
}
let authResult: {
authenticated: boolean;
tokenVersion: number;
role: AdminRole;
memberId: string;
email: string;
};
try {
authResult = await authenticateAdminCredentials(email, password);
} catch (err) {
if (err instanceof AdminSetupRequiredError) {
return NextResponse.json(
{
error: "Initial admin setup is required before signing in.",
code: err.code,
},
{ status: 409 },
);
}
return NextResponse.json(
{ error: "Admin authentication is temporarily unavailable." },
{ status: 503 },
);
}
if (!authResult.authenticated) {
return NextResponse.json(
{ error: "Invalid email or password." },
{ status: 401 },
);
}
try {
await clearAdminLoginAttempts(email, sessionSecret, clientIp);
} 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;
const effectiveEmail = authResult.email || email;
const token = await createAdminSessionToken(
effectiveEmail,
sessionSecret,
expiresAt,
authResult.tokenVersion,
authResult.role,
authResult.memberId,
);
await recordAdminAuditLogAsync({
actorEmail: effectiveEmail,
actorRole: authResult.role || "owner",
action: "ADMIN_LOGIN",
target: effectiveEmail,
ipAddress: clientIp,
metadata: { remember },
});
const response = NextResponse.json({
success: true,
role: authResult.role || "owner",
});
response.cookies.set(
SESSION_COOKIE,
token,
getSessionCookieOptions(request, maxAge),
);
return response;
2026-10-23 19:56:38 -07:00
}