2026-11-16 22:12:48 +06:00
|
|
|
import {
|
2026-11-20 18:48:54 +06:00
|
|
|
type AdminRole,
|
2026-11-16 22:12:48 +06:00
|
|
|
createAdminSessionToken,
|
2026-11-18 05:18:55 +06:00
|
|
|
getAdminSessionSecret,
|
2026-11-16 22:12:48 +06:00
|
|
|
getSessionCookieOptions,
|
|
|
|
|
} from "@/core/auth/adminSession";
|
2026-10-25 14:16:16 -07:00
|
|
|
import {
|
2026-11-19 20:22:01 +06:00
|
|
|
AdminSetupRequiredError,
|
2026-11-18 05:18:55 +06:00
|
|
|
authenticateAdminCredentials,
|
2026-10-25 14:16:16 -07:00
|
|
|
clearAdminLoginAttempts,
|
|
|
|
|
consumeAdminLoginAttempt,
|
|
|
|
|
} from "@/core/auth/adminUsers";
|
2026-11-21 00:06:55 +06:00
|
|
|
import { SettingsManager } from "@/core/adminSettings/settingsManager";
|
2026-11-20 18:48:54 +06:00
|
|
|
import { recordAdminAuditLogAsync } from "@/core/auth/auditLog";
|
2026-11-19 20:22:01 +06:00
|
|
|
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) {
|
2026-11-18 05:18:55 +06:00
|
|
|
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 },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-18 05:18:55 +06:00
|
|
|
let body: Record<string, unknown>;
|
2026-10-23 19:56:38 -07:00
|
|
|
try {
|
2026-11-18 05:18:55 +06:00
|
|
|
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 },
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-11-18 05:18:55 +06:00
|
|
|
|
|
|
|
|
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) {
|
2026-11-21 00:06:55 +06:00
|
|
|
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,
|
|
|
|
|
},
|
|
|
|
|
});
|
2026-11-18 05:18:55 +06:00
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "Too many sign-in attempts. Try again later." },
|
|
|
|
|
{
|
|
|
|
|
status: 429,
|
|
|
|
|
headers: {
|
|
|
|
|
"Retry-After": String(throttleDecision.retryAfterSeconds),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-20 18:48:54 +06:00
|
|
|
let authResult: {
|
|
|
|
|
authenticated: boolean;
|
|
|
|
|
tokenVersion: number;
|
|
|
|
|
role: AdminRole;
|
|
|
|
|
memberId: string;
|
|
|
|
|
email: string;
|
|
|
|
|
};
|
2026-11-18 05:18:55 +06:00
|
|
|
try {
|
|
|
|
|
authResult = await authenticateAdminCredentials(email, password);
|
2026-11-19 20:22:01 +06:00
|
|
|
} catch (err) {
|
|
|
|
|
if (err instanceof AdminSetupRequiredError) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{
|
|
|
|
|
error: "Initial admin setup is required before signing in.",
|
|
|
|
|
code: err.code,
|
|
|
|
|
},
|
|
|
|
|
{ status: 409 },
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-11-18 05:18:55 +06:00
|
|
|
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;
|
2026-11-20 18:48:54 +06:00
|
|
|
const effectiveEmail = authResult.email || email;
|
2026-11-18 05:18:55 +06:00
|
|
|
const token = await createAdminSessionToken(
|
2026-11-20 18:48:54 +06:00
|
|
|
effectiveEmail,
|
2026-11-18 05:18:55 +06:00
|
|
|
sessionSecret,
|
|
|
|
|
expiresAt,
|
|
|
|
|
authResult.tokenVersion,
|
2026-11-20 18:48:54 +06:00
|
|
|
authResult.role,
|
|
|
|
|
authResult.memberId,
|
2026-11-18 05:18:55 +06:00
|
|
|
);
|
2026-11-20 18:48:54 +06:00
|
|
|
|
|
|
|
|
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",
|
|
|
|
|
});
|
2026-11-18 05:18:55 +06:00
|
|
|
|
|
|
|
|
response.cookies.set(
|
|
|
|
|
SESSION_COOKIE,
|
|
|
|
|
token,
|
|
|
|
|
getSessionCookieOptions(request, maxAge),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return response;
|
2026-10-23 19:56:38 -07:00
|
|
|
}
|