Files
DataStack/app/api/auth/profile/route.ts
T

189 lines
5.5 KiB
TypeScript
Raw Normal View History

2026-11-16 23:10:44 +06:00
import {
createAdminSessionToken,
formatAdminRoleLabel,
2026-11-16 23:10:44 +06:00
getSessionCookieOptions,
} from "@/core/auth/adminSession";
import {
getAdminAccountInfo,
updateAdminAccount,
verifyRequestAdminSession,
2026-11-16 23:10:44 +06:00
} from "@/core/auth/adminUsers";
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) {
const token = request.cookies.get(SESSION_COOKIE)?.value;
const auth = await verifyRequestAdminSession(token);
2026-11-16 23:10:44 +06:00
if (!auth.valid) {
const status = auth.reason === "UNAUTHORIZED" ? 401 : 503;
const code =
auth.reason === "UNAUTHORIZED" ? "SESSION_EXPIRED" : "AUTH_UNAVAILABLE";
const message =
auth.reason === "UNAUTHORIZED"
? "Authentication required."
: "Admin authentication is temporarily unavailable.";
return NextResponse.json({ error: message, code }, { status });
2026-11-16 23:10:44 +06:00
}
try {
const account = await getAdminAccountInfo(auth.session.email);
const effectiveRole = auth.session.role || account.role || "owner";
return NextResponse.json({
profile: {
email: account.email,
role: formatAdminRoleLabel(effectiveRole),
roleKey: effectiveRole,
memberId: auth.session.memberId || account.memberId || "owner-1",
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) {
const token = request.cookies.get(SESSION_COOKIE)?.value;
const auth = await verifyRequestAdminSession(token);
2026-11-16 23:10:44 +06:00
if (!auth.valid) {
const status = auth.reason === "UNAUTHORIZED" ? 401 : 503;
const code =
auth.reason === "UNAUTHORIZED" ? "SESSION_EXPIRED" : "AUTH_UNAVAILABLE";
const message =
auth.reason === "UNAUTHORIZED"
? "Authentication required."
: "Admin authentication is temporarily unavailable.";
return NextResponse.json({ error: message, code }, { status });
}
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(
{ 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()
: 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 },
);
}
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) {
return NextResponse.json(
{ error: result.error, code: "INVALID_PASSWORD" },
{ status: 401 },
);
2026-11-16 23:10:44 +06:00
}
const remainingSeconds = Math.max(
SESSION_SECONDS,
Math.ceil((auth.session.expiresAt - Date.now()) / 1000),
);
const expiresAt = Date.now() + remainingSeconds * 1000;
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,
auth.sessionSecret,
2026-11-16 23:10:44 +06:00
expiresAt,
result.tokenVersion,
effectiveRole,
effectiveMemberId,
2026-11-16 23:10:44 +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,
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,
getSessionCookieOptions(request, remainingSeconds),
2026-11-16 23:10:44 +06:00
);
return response;
} catch {
return NextResponse.json(
{ error: "Unable to update profile." },
{ status: 500 },
2026-11-16 23:10:44 +06:00
);
}
}