makearmy-app/app/api/account/password/route.ts

83 lines
3.2 KiB
TypeScript
Raw Normal View History

2025-09-30 00:56:23 -04:00
// app/api/account/password/route.ts
import { NextResponse } from "next/server";
import { requireBearer } from "@/app/api/_lib/auth";
2025-09-30 20:55:19 -04:00
import { loginDirectus } from "@/lib/directus";
2025-09-30 00:56:23 -04:00
2025-09-30 09:29:44 -04:00
export const runtime = "nodejs";
2025-09-30 00:56:23 -04:00
const API = (process.env.NEXT_PUBLIC_API_BASE_URL || "").replace(/\/$/, "");
2025-09-30 09:29:44 -04:00
const bad = (m: string, c = 400) => NextResponse.json({ error: m }, { status: c });
async function handle(req: Request) {
const bearer = requireBearer(req);
const body = await req.json().catch(() => ({}));
2025-09-30 17:04:37 -04:00
const current = String(body?.current ?? body?.current_password ?? "").trim();
const next = String(body?.next ?? body?.new_password ?? "").trim();
2025-09-30 09:29:44 -04:00
if (!current || !next) return bad("Missing current and/or new password");
if (next.length < 8) return bad("Password must be at least 8 characters");
2025-09-30 22:45:42 -04:00
// 1) Load current user to get provider + identifiers
const meRes = await fetch(`${API}/users/me?fields=id,email,username,provider`, {
2025-09-30 20:49:37 -04:00
headers: { Authorization: `Bearer ${bearer}`, Accept: "application/json" },
cache: "no-store",
});
2025-09-30 20:55:19 -04:00
const me = await meRes.json().catch(() => ({}));
if (!meRes.ok) {
2025-09-30 20:49:37 -04:00
return NextResponse.json(
2025-09-30 20:55:19 -04:00
{ error: "Could not verify user", debug: me?.errors?.[0]?.message || meRes.statusText },
{ status: meRes.status }
2025-09-30 20:49:37 -04:00
);
}
2025-09-30 20:55:19 -04:00
const provider: string = me?.data?.provider ?? me?.provider ?? "local";
2025-09-30 22:45:42 -04:00
const email: string | undefined = me?.data?.email ?? me?.email ?? undefined;
const username: string | undefined = me?.data?.username ?? me?.username ?? undefined;
2025-09-30 20:55:19 -04:00
2025-09-30 22:45:42 -04:00
if (provider !== "local") {
2025-09-30 20:55:19 -04:00
return NextResponse.json(
2025-09-30 22:45:42 -04:00
{ error: "Password managed by external provider", debug: `provider=${provider}` },
2025-09-30 20:55:19 -04:00
{ status: 400 }
);
}
2025-09-30 22:45:42 -04:00
// 2) Verify CURRENT password by logging in with email OR username
const identifier = email || username;
if (!identifier) {
2025-09-30 20:49:37 -04:00
return NextResponse.json(
2025-09-30 22:45:42 -04:00
{ error: "No login identifier available for this user", debug: "missing email and username" },
2025-09-30 20:49:37 -04:00
{ status: 400 }
);
}
2025-09-30 22:45:42 -04:00
const auth = await loginDirectus(identifier, current).catch(() => null);
2025-09-30 20:55:19 -04:00
const access = auth?.access_token ?? auth?.data?.access_token;
if (!access) {
return NextResponse.json({ error: "Current password is incorrect" }, { status: 401 });
}
2025-09-30 20:49:37 -04:00
2025-09-30 22:45:42 -04:00
// 3) Update password using ONLY the 'password' field
2025-09-30 20:55:19 -04:00
const patchRes = await fetch(`${API}/users/me`, {
2025-09-30 09:29:44 -04:00
method: "PATCH",
headers: {
2025-09-30 17:04:37 -04:00
Authorization: `Bearer ${bearer}`,
2025-09-30 09:29:44 -04:00
"Content-Type": "application/json",
2025-09-30 20:49:37 -04:00
Accept: "application/json",
2025-09-30 09:29:44 -04:00
},
2025-09-30 20:55:19 -04:00
body: JSON.stringify({ password: next }),
2025-09-30 09:29:44 -04:00
});
2025-09-30 20:55:19 -04:00
const j = await patchRes.json().catch(() => ({}));
if (!patchRes.ok) {
2025-09-30 20:49:37 -04:00
const reason =
2025-09-30 20:55:19 -04:00
j?.errors?.[0]?.message || j?.error || (typeof j === "string" ? j : "") || "Password change failed";
return NextResponse.json({ error: reason }, { status: patchRes.status });
2025-09-30 09:29:44 -04:00
}
2025-09-30 00:56:23 -04:00
2025-09-30 09:29:44 -04:00
return NextResponse.json({ ok: true });
2025-09-30 00:56:23 -04:00
}
2025-09-30 20:49:37 -04:00
export async function POST(req: Request) { return handle(req); }
export async function PATCH(req: Request) { return handle(req); }