change password bug fix
This commit is contained in:
parent
6abe450f1d
commit
d090ff44f2
1 changed files with 15 additions and 14 deletions
|
|
@ -18,8 +18,8 @@ async function handle(req: Request) {
|
|||
if (!current || !next) return bad("Missing current and/or new password");
|
||||
if (next.length < 8) return bad("Password must be at least 8 characters");
|
||||
|
||||
// 1) Load the current user to get email + provider
|
||||
const meRes = await fetch(`${API}/users/me?fields=id,email,provider`, {
|
||||
// 1) Load current user to get provider + identifiers
|
||||
const meRes = await fetch(`${API}/users/me?fields=id,email,username,provider`, {
|
||||
headers: { Authorization: `Bearer ${bearer}`, Accept: "application/json" },
|
||||
cache: "no-store",
|
||||
});
|
||||
|
|
@ -31,15 +31,9 @@ async function handle(req: Request) {
|
|||
);
|
||||
}
|
||||
|
||||
const email: string | undefined = me?.data?.email ?? me?.email;
|
||||
const provider: string = me?.data?.provider ?? me?.provider ?? "local";
|
||||
|
||||
if (!email) {
|
||||
return NextResponse.json(
|
||||
{ error: "User email not available", debug: "users/me returned no email" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
const email: string | undefined = me?.data?.email ?? me?.email ?? undefined;
|
||||
const username: string | undefined = me?.data?.username ?? me?.username ?? undefined;
|
||||
|
||||
if (provider !== "local") {
|
||||
return NextResponse.json(
|
||||
|
|
@ -48,15 +42,22 @@ async function handle(req: Request) {
|
|||
);
|
||||
}
|
||||
|
||||
// 2) Verify the CURRENT password by logging in to Directus
|
||||
const auth = await loginDirectus(email, current).catch(() => null);
|
||||
// 2) Verify CURRENT password by logging in with email OR username
|
||||
const identifier = email || username;
|
||||
if (!identifier) {
|
||||
return NextResponse.json(
|
||||
{ error: "No login identifier available for this user", debug: "missing email and username" },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
const auth = await loginDirectus(identifier, current).catch(() => null);
|
||||
const access = auth?.access_token ?? auth?.data?.access_token;
|
||||
if (!access) {
|
||||
// We’ve confirmed the “current” really doesn’t match
|
||||
return NextResponse.json({ error: "Current password is incorrect" }, { status: 401 });
|
||||
}
|
||||
|
||||
// 3) Update password using ONLY the 'password' field (avoid non-existent keys)
|
||||
// 3) Update password using ONLY the 'password' field
|
||||
const patchRes = await fetch(`${API}/users/me`, {
|
||||
method: "PATCH",
|
||||
headers: {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue