feat(auth): harden session management and security

- Implement server-side session revocation on logout
- Add IP extraction utility and admin setup error handling
- Improve error codes and response consistency for auth endpoints
- Refine landing page redirect logic to handle transient DB outages
- Update deployment documentation and configuration defaults
This commit is contained in:
Team Dark Devil committed 2026-11-19 20:22:01 +06:00
1 parent 935f12a1e9
commit 89e4d68690
81 files changed
+11530 -6916

No files matched your search

+59 -7
View File
@@ -4,14 +4,20 @@ import {
getSessionCookieOptions,
} from "@/core/auth/adminSession";
import {
AdminDatabaseRequiredError,
clearAdminLoginAttempts,
consumeAdminLoginAttempt,
createInitialAdmin,
isAdminSetupComplete,
requiresPersistentAdminDatabase,
verifyAdminSetupKey,
} from "@/core/auth/adminUsers";
import { extractClientIp } from "@/lib/adminAuth";
import { NextRequest, NextResponse } from "next/server";
const SESSION_COOKIE = "datastack_admin_session";
const SESSION_SECONDS = 8 * 60 * 60;
const SETUP_THROTTLE_BUCKET = "__admin_setup__";
export const runtime = "nodejs";
export const dynamic = "force-dynamic";
@@ -29,6 +35,17 @@ export async function POST(request: NextRequest) {
);
}
if (requiresPersistentAdminDatabase()) {
return NextResponse.json(
{
error:
"PostgreSQL DATABASE_URL must be configured in production before initial admin setup.",
code: "DATABASE_NOT_CONFIGURED",
},
{ status: 503 },
);
}
let body: Record<string, unknown>;
try {
const parsed = await request.json();
@@ -61,12 +78,7 @@ export async function POST(request: NextRequest) {
{ status: 400 },
);
}
if (!verifyAdminSetupKey(setupKey)) {
return NextResponse.json(
{ error: "The installation key is invalid." },
{ status: 403 },
);
}
if (await isAdminSetupComplete()) {
return NextResponse.json(
{ error: "Initial admin setup has already been completed." },
@@ -74,6 +86,34 @@ export async function POST(request: NextRequest) {
);
}
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 },
);
}
const created = await createInitialAdmin(email, password);
if (!created) {
return NextResponse.json(
@@ -82,6 +122,12 @@ export async function POST(request: NextRequest) {
);
}
await clearAdminLoginAttempts(
SETUP_THROTTLE_BUCKET,
sessionSecret,
clientIp,
).catch(() => undefined);
const token = await createAdminSessionToken(
email,
sessionSecret,
@@ -95,7 +141,13 @@ export async function POST(request: NextRequest) {
getSessionCookieOptions(request, SESSION_SECONDS),
);
return response;
} catch {
} catch (err) {
if (err instanceof AdminDatabaseRequiredError) {
return NextResponse.json(
{ error: err.message, code: err.code },
{ status: 503 },
);
}
return NextResponse.json(
{ error: "Admin setup is temporarily unavailable." },
{ status: 503 },