This commit is contained in:
eric
2026-03-12 19:54:58 -05:00
parent 14bbd3b306
commit f66859c032
31 changed files with 1132 additions and 312 deletions

View File

@@ -3,6 +3,7 @@
*/
import { NextRequest, NextResponse } from "next/server";
import { getPocketBaseConfig } from "@/config/services.config";
import { getAdminToken } from "@/app/lib/pocketbase/admin";
const COOKIE_NAME = "pb_session";
const COOKIE_NEEDS_PW = "meetup_needs_password_change";
@@ -38,24 +39,35 @@ export async function POST(request: NextRequest) {
}
const pb = getPocketBaseConfig();
const res = await fetch(`${pb.url}/api/collections/users/records/${auth.userId}`, {
const patchBody = { password: newPassword, passwordConfirm };
let res = await fetch(`${pb.url}/api/collections/users/records/${auth.userId}`, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${auth.token}`,
},
body: JSON.stringify({
password: newPassword,
passwordConfirm: passwordConfirm,
}),
body: JSON.stringify(patchBody),
});
if (!res.ok) {
const adminToken = await getAdminToken();
if (adminToken) {
res = await fetch(`${pb.url}/api/collections/users/records/${auth.userId}`, {
method: "PATCH",
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${adminToken}`,
},
body: JSON.stringify(patchBody),
});
}
}
if (!res.ok) {
const err = await res.json().catch(() => ({}));
return NextResponse.json(
{ ok: false, error: err?.message || "修改失败" },
{ status: 400 }
);
const msg = err?.message || (err?.data && typeof err.data === "object" && Object.values(err.data)[0] && (Object.values(err.data)[0] as { message?: string })?.message) || "修改失败";
return NextResponse.json({ ok: false, error: msg }, { status: 400 });
}
const r = NextResponse.json({ ok: true });