import { json, MIN_PASSWORD_LENGTH, readJsonBody, requireAdmin, setPassword, verifyPassword } from "../../../_lib/auth"; import { readAuth } from "../../../_lib/store"; export const dynamic = "force-dynamic"; /** Changes the password; signs out every other session. */ export async function POST(request: Request) { const denied = await requireAdmin(request, { mutating: true }); if (denied) return denied; const body = await readJsonBody<{ currentPassword?: string; newPassword?: string }>(request); const current = typeof body?.currentPassword === "string" ? body.currentPassword : ""; const next = typeof body?.newPassword === "string" ? body.newPassword : ""; if (!(await verifyPassword(current, (await readAuth()).passwordHash))) return json({ error: "Current password is wrong." }, 401); if (next.length < MIN_PASSWORD_LENGTH || next.length > 256) return json({ error: `Use at least ${MIN_PASSWORD_LENGTH} characters.` }, 400); return json({ ok: true }, 200, { "Set-Cookie": await setPassword(next) }); }