2026-11-16 22:12:48 +06:00
|
|
|
import {
|
|
|
|
|
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 06:12:47 -07:00
|
|
|
import {
|
2026-11-19 20:22:01 +06:00
|
|
|
AdminDatabaseRequiredError,
|
|
|
|
|
clearAdminLoginAttempts,
|
|
|
|
|
consumeAdminLoginAttempt,
|
2026-10-25 06:12:47 -07:00
|
|
|
createInitialAdmin,
|
|
|
|
|
isAdminSetupComplete,
|
2026-11-19 20:22:01 +06:00
|
|
|
requiresPersistentAdminDatabase,
|
2026-10-25 06:12:47 -07:00
|
|
|
verifyAdminSetupKey,
|
|
|
|
|
} from "@/core/auth/adminUsers";
|
2026-11-19 20:22:01 +06:00
|
|
|
import { extractClientIp } from "@/lib/adminAuth";
|
2026-10-25 06:12:47 -07:00
|
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
|
|
|
|
|
|
const SESSION_COOKIE = "datastack_admin_session";
|
|
|
|
|
const SESSION_SECONDS = 8 * 60 * 60;
|
2026-11-19 20:22:01 +06:00
|
|
|
const SETUP_THROTTLE_BUCKET = "__admin_setup__";
|
2026-10-25 06:12:47 -07:00
|
|
|
|
|
|
|
|
export const runtime = "nodejs";
|
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
|
|
|
|
|
|
function validEmail(email: string) {
|
|
|
|
|
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function POST(request: NextRequest) {
|
2026-11-18 05:18:55 +06:00
|
|
|
const sessionSecret = getAdminSessionSecret();
|
2026-10-25 06:12:47 -07:00
|
|
|
if (!sessionSecret || sessionSecret.length < 32) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "Admin session configuration is unavailable." },
|
|
|
|
|
{ status: 503 },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-19 20:22:01 +06:00
|
|
|
if (requiresPersistentAdminDatabase()) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{
|
|
|
|
|
error:
|
|
|
|
|
"PostgreSQL DATABASE_URL must be configured in production before initial admin setup.",
|
|
|
|
|
code: "DATABASE_NOT_CONFIGURED",
|
|
|
|
|
},
|
|
|
|
|
{ status: 503 },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-18 05:18:55 +06:00
|
|
|
let body: Record<string, unknown>;
|
|
|
|
|
try {
|
|
|
|
|
const parsed = await request.json();
|
|
|
|
|
body =
|
|
|
|
|
parsed && typeof parsed === "object" && !Array.isArray(parsed)
|
|
|
|
|
? (parsed as Record<string, unknown>)
|
|
|
|
|
: {};
|
|
|
|
|
} catch {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "Invalid setup request payload." },
|
|
|
|
|
{ status: 400 },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-25 06:12:47 -07:00
|
|
|
try {
|
|
|
|
|
const email =
|
|
|
|
|
typeof body.email === "string" ? body.email.trim().toLowerCase() : "";
|
|
|
|
|
const password = typeof body.password === "string" ? body.password : "";
|
|
|
|
|
const setupKey = typeof body.setupKey === "string" ? body.setupKey : "";
|
|
|
|
|
|
|
|
|
|
if (!validEmail(email)) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "Enter a valid email address." },
|
|
|
|
|
{ status: 400 },
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-11-17 20:05:00 +06:00
|
|
|
if (password.length < 8 || password.length > 256) {
|
2026-10-25 06:12:47 -07:00
|
|
|
return NextResponse.json(
|
2026-11-17 20:05:00 +06:00
|
|
|
{ error: "Choose a password between 8 and 256 characters." },
|
2026-10-25 06:12:47 -07:00
|
|
|
{ status: 400 },
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-11-19 20:22:01 +06:00
|
|
|
|
2026-10-25 06:12:47 -07:00
|
|
|
if (await isAdminSetupComplete()) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "Initial admin setup has already been completed." },
|
|
|
|
|
{ status: 409 },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-19 20:22:01 +06:00
|
|
|
const clientIp = extractClientIp(request);
|
|
|
|
|
const throttleDecision = await consumeAdminLoginAttempt(
|
|
|
|
|
SETUP_THROTTLE_BUCKET,
|
|
|
|
|
sessionSecret,
|
|
|
|
|
clientIp,
|
|
|
|
|
);
|
|
|
|
|
if (!throttleDecision.allowed) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{
|
|
|
|
|
error: "Too many setup attempts. Try again later.",
|
|
|
|
|
code: "TOO_MANY_ATTEMPTS",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
status: 429,
|
|
|
|
|
headers: {
|
|
|
|
|
"Retry-After": String(throttleDecision.retryAfterSeconds),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!verifyAdminSetupKey(setupKey)) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "The installation key is invalid." },
|
|
|
|
|
{ status: 403 },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-25 06:12:47 -07:00
|
|
|
const created = await createInitialAdmin(email, password);
|
|
|
|
|
if (!created) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "Initial admin setup has already been completed." },
|
|
|
|
|
{ status: 409 },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-19 20:22:01 +06:00
|
|
|
await clearAdminLoginAttempts(
|
|
|
|
|
SETUP_THROTTLE_BUCKET,
|
|
|
|
|
sessionSecret,
|
|
|
|
|
clientIp,
|
|
|
|
|
).catch(() => undefined);
|
|
|
|
|
|
2026-10-25 06:12:47 -07:00
|
|
|
const token = await createAdminSessionToken(
|
|
|
|
|
email,
|
|
|
|
|
sessionSecret,
|
|
|
|
|
Date.now() + SESSION_SECONDS * 1000,
|
2026-11-18 05:18:55 +06:00
|
|
|
1,
|
2026-10-25 06:12:47 -07:00
|
|
|
);
|
|
|
|
|
const response = NextResponse.json({ success: true });
|
2026-11-16 22:12:48 +06:00
|
|
|
response.cookies.set(
|
|
|
|
|
SESSION_COOKIE,
|
|
|
|
|
token,
|
|
|
|
|
getSessionCookieOptions(request, SESSION_SECONDS),
|
|
|
|
|
);
|
2026-10-25 06:12:47 -07:00
|
|
|
return response;
|
2026-11-19 20:22:01 +06:00
|
|
|
} catch (err) {
|
|
|
|
|
if (err instanceof AdminDatabaseRequiredError) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: err.message, code: err.code },
|
|
|
|
|
{ status: 503 },
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-10-25 06:12:47 -07:00
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "Admin setup is temporarily unavailable." },
|
|
|
|
|
{ status: 503 },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|