2026-11-16 23:10:44 +06:00
|
|
|
import {
|
|
|
|
|
createAdminSessionToken,
|
2026-11-20 18:48:54 +06:00
|
|
|
formatAdminRoleLabel,
|
2026-11-16 23:10:44 +06:00
|
|
|
getSessionCookieOptions,
|
|
|
|
|
} from "@/core/auth/adminSession";
|
|
|
|
|
import {
|
|
|
|
|
getAdminAccountInfo,
|
|
|
|
|
updateAdminAccount,
|
2026-11-18 05:18:55 +06:00
|
|
|
verifyRequestAdminSession,
|
2026-11-16 23:10:44 +06:00
|
|
|
} from "@/core/auth/adminUsers";
|
2026-11-20 18:48:54 +06:00
|
|
|
import { recordAdminAuditLogAsync } from "@/core/auth/auditLog";
|
|
|
|
|
import { extractClientIp } from "@/lib/adminAuth";
|
2026-11-16 23:10:44 +06:00
|
|
|
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
|
|
|
|
|
|
const SESSION_COOKIE = "datastack_admin_session";
|
|
|
|
|
const SESSION_SECONDS = 8 * 60 * 60;
|
|
|
|
|
|
|
|
|
|
function validEmail(email: string) {
|
|
|
|
|
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function GET(request: NextRequest) {
|
2026-11-18 05:18:55 +06:00
|
|
|
const token = request.cookies.get(SESSION_COOKIE)?.value;
|
|
|
|
|
const auth = await verifyRequestAdminSession(token);
|
2026-11-16 23:10:44 +06:00
|
|
|
|
2026-11-18 05:18:55 +06:00
|
|
|
if (!auth.valid) {
|
|
|
|
|
const status = auth.reason === "UNAUTHORIZED" ? 401 : 503;
|
2026-11-19 20:22:01 +06:00
|
|
|
const code =
|
|
|
|
|
auth.reason === "UNAUTHORIZED" ? "SESSION_EXPIRED" : "AUTH_UNAVAILABLE";
|
2026-11-18 05:18:55 +06:00
|
|
|
const message =
|
|
|
|
|
auth.reason === "UNAUTHORIZED"
|
|
|
|
|
? "Authentication required."
|
|
|
|
|
: "Admin authentication is temporarily unavailable.";
|
2026-11-19 20:22:01 +06:00
|
|
|
return NextResponse.json({ error: message, code }, { status });
|
2026-11-16 23:10:44 +06:00
|
|
|
}
|
|
|
|
|
|
2026-11-18 05:18:55 +06:00
|
|
|
try {
|
|
|
|
|
const account = await getAdminAccountInfo(auth.session.email);
|
2026-11-20 18:48:54 +06:00
|
|
|
const effectiveRole = auth.session.role || account.role || "owner";
|
2026-11-18 05:18:55 +06:00
|
|
|
return NextResponse.json({
|
|
|
|
|
profile: {
|
|
|
|
|
email: account.email,
|
2026-11-20 18:48:54 +06:00
|
|
|
role: formatAdminRoleLabel(effectiveRole),
|
|
|
|
|
roleKey: effectiveRole,
|
|
|
|
|
memberId: auth.session.memberId || account.memberId || "owner-1",
|
2026-11-18 05:18:55 +06:00
|
|
|
storageMode: account.storageMode,
|
|
|
|
|
createdAt: account.createdAt,
|
|
|
|
|
sessionExpiresAt: new Date(auth.session.expiresAt).toISOString(),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
} catch {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "Unable to load admin profile." },
|
|
|
|
|
{ status: 503 },
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-11-16 23:10:44 +06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function PUT(request: NextRequest) {
|
2026-11-18 05:18:55 +06:00
|
|
|
const token = request.cookies.get(SESSION_COOKIE)?.value;
|
|
|
|
|
const auth = await verifyRequestAdminSession(token);
|
2026-11-16 23:10:44 +06:00
|
|
|
|
2026-11-18 05:18:55 +06:00
|
|
|
if (!auth.valid) {
|
|
|
|
|
const status = auth.reason === "UNAUTHORIZED" ? 401 : 503;
|
2026-11-19 20:22:01 +06:00
|
|
|
const code =
|
|
|
|
|
auth.reason === "UNAUTHORIZED" ? "SESSION_EXPIRED" : "AUTH_UNAVAILABLE";
|
2026-11-18 05:18:55 +06:00
|
|
|
const message =
|
|
|
|
|
auth.reason === "UNAUTHORIZED"
|
|
|
|
|
? "Authentication required."
|
|
|
|
|
: "Admin authentication is temporarily unavailable.";
|
2026-11-19 20:22:01 +06:00
|
|
|
return NextResponse.json({ error: message, code }, { status });
|
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 {
|
2026-11-16 23:10:44 +06:00
|
|
|
return NextResponse.json(
|
2026-11-18 05:18:55 +06:00
|
|
|
{ error: "Invalid profile payload." },
|
|
|
|
|
{ status: 400 },
|
2026-11-16 23:10:44 +06:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const nextEmail =
|
|
|
|
|
typeof body.email === "string"
|
|
|
|
|
? body.email.trim().toLowerCase()
|
2026-11-18 05:18:55 +06:00
|
|
|
: auth.session.email;
|
2026-11-16 23:10:44 +06:00
|
|
|
const currentPassword =
|
|
|
|
|
typeof body.currentPassword === "string" ? body.currentPassword : "";
|
|
|
|
|
const newPassword =
|
|
|
|
|
typeof body.newPassword === "string" ? body.newPassword : "";
|
|
|
|
|
|
|
|
|
|
if (!validEmail(nextEmail)) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "Enter a valid email address." },
|
|
|
|
|
{ status: 400 },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!currentPassword) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "Current password is required to save changes." },
|
|
|
|
|
{ status: 400 },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (newPassword && (newPassword.length < 8 || newPassword.length > 256)) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "New password must be between 8 and 256 characters." },
|
|
|
|
|
{ status: 400 },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-18 05:18:55 +06:00
|
|
|
const account = await getAdminAccountInfo(auth.session.email);
|
2026-11-16 23:10:44 +06:00
|
|
|
const result = await updateAdminAccount({
|
|
|
|
|
currentEmail: account.email,
|
|
|
|
|
currentPassword,
|
|
|
|
|
nextEmail,
|
|
|
|
|
nextPassword: newPassword || undefined,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!result.ok) {
|
2026-11-19 20:22:01 +06:00
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: result.error, code: "INVALID_PASSWORD" },
|
|
|
|
|
{ status: 401 },
|
|
|
|
|
);
|
2026-11-16 23:10:44 +06:00
|
|
|
}
|
|
|
|
|
|
2026-11-17 20:05:00 +06:00
|
|
|
const remainingSeconds = Math.max(
|
|
|
|
|
SESSION_SECONDS,
|
2026-11-18 05:18:55 +06:00
|
|
|
Math.ceil((auth.session.expiresAt - Date.now()) / 1000),
|
2026-11-17 20:05:00 +06:00
|
|
|
);
|
|
|
|
|
const expiresAt = Date.now() + remainingSeconds * 1000;
|
2026-11-20 18:48:54 +06:00
|
|
|
const effectiveRole = result.role || auth.session.role || "owner";
|
|
|
|
|
const effectiveMemberId =
|
|
|
|
|
result.memberId || auth.session.memberId || "owner-1";
|
2026-11-16 23:10:44 +06:00
|
|
|
const nextToken = await createAdminSessionToken(
|
|
|
|
|
result.email,
|
2026-11-18 05:18:55 +06:00
|
|
|
auth.sessionSecret,
|
2026-11-16 23:10:44 +06:00
|
|
|
expiresAt,
|
2026-11-18 05:18:55 +06:00
|
|
|
result.tokenVersion,
|
2026-11-20 18:48:54 +06:00
|
|
|
effectiveRole,
|
|
|
|
|
effectiveMemberId,
|
2026-11-16 23:10:44 +06:00
|
|
|
);
|
|
|
|
|
|
2026-11-20 18:48:54 +06:00
|
|
|
await recordAdminAuditLogAsync({
|
|
|
|
|
actorEmail: result.email,
|
|
|
|
|
actorRole: effectiveRole,
|
|
|
|
|
action: newPassword ? "ADMIN_PASSWORD_UPDATED" : "ADMIN_PROFILE_UPDATED",
|
|
|
|
|
target: result.email,
|
|
|
|
|
ipAddress: extractClientIp(request),
|
|
|
|
|
metadata: {
|
|
|
|
|
emailChanged: result.email !== account.email,
|
|
|
|
|
passwordRotated: Boolean(newPassword),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2026-11-16 23:10:44 +06:00
|
|
|
const response = NextResponse.json({
|
|
|
|
|
success: true,
|
|
|
|
|
profile: {
|
|
|
|
|
email: result.email,
|
2026-11-20 18:48:54 +06:00
|
|
|
role: formatAdminRoleLabel(effectiveRole),
|
|
|
|
|
roleKey: effectiveRole,
|
|
|
|
|
memberId: effectiveMemberId,
|
2026-11-16 23:10:44 +06:00
|
|
|
storageMode: account.storageMode,
|
|
|
|
|
createdAt: account.createdAt,
|
|
|
|
|
sessionExpiresAt: new Date(expiresAt).toISOString(),
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
response.cookies.set(
|
|
|
|
|
SESSION_COOKIE,
|
|
|
|
|
nextToken,
|
2026-11-17 20:05:00 +06:00
|
|
|
getSessionCookieOptions(request, remainingSeconds),
|
2026-11-16 23:10:44 +06:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
} catch {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "Unable to update profile." },
|
2026-11-18 05:18:55 +06:00
|
|
|
{ status: 500 },
|
2026-11-16 23:10:44 +06:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|