Centralize cookie security options to dynamically detect HTTPS, ensure secure transport, and update client-side auth handlers to use explicit credential inclusion for cookie-based sessions.
113 lines
2.9 KiB
TypeScript
113 lines
2.9 KiB
TypeScript
import {
|
|
createAdminSessionToken,
|
|
getSessionCookieOptions,
|
|
} from "@/core/auth/adminSession";
|
|
import {
|
|
clearAdminLoginAttempts,
|
|
consumeAdminLoginAttempt,
|
|
verifyAdminCredentials,
|
|
} from "@/core/auth/adminUsers";
|
|
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 =
|
|
process.env.DATASTACK_ADMIN_SESSION_SECRET ||
|
|
"datastack-admin-session-default-secret-32-chars";
|
|
|
|
if (!sessionSecret || sessionSecret.length < 32) {
|
|
return NextResponse.json(
|
|
{
|
|
error: "Admin authentication is not configured.",
|
|
},
|
|
{ status: 503 },
|
|
);
|
|
}
|
|
|
|
try {
|
|
const body = await request.json();
|
|
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 },
|
|
);
|
|
}
|
|
|
|
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;
|
|
try {
|
|
authenticated = await verifyAdminCredentials(email, password);
|
|
} catch {
|
|
return NextResponse.json(
|
|
{ error: "Admin authentication is temporarily unavailable." },
|
|
{ status: 503 },
|
|
);
|
|
}
|
|
|
|
if (!authenticated) {
|
|
return NextResponse.json(
|
|
{ error: "Invalid email or password." },
|
|
{ status: 401 },
|
|
);
|
|
}
|
|
|
|
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;
|
|
const token = await createAdminSessionToken(
|
|
email,
|
|
sessionSecret,
|
|
expiresAt,
|
|
);
|
|
const response = NextResponse.json({ success: true });
|
|
|
|
response.cookies.set(
|
|
SESSION_COOKIE,
|
|
token,
|
|
getSessionCookieOptions(request, maxAge),
|
|
);
|
|
|
|
return response;
|
|
} catch {
|
|
return NextResponse.json(
|
|
{ error: "Invalid login request." },
|
|
{ status: 400 },
|
|
);
|
|
}
|
|
}
|