Refactor password update functionality

This commit is contained in:
daniel31x13
2024-05-21 07:08:08 -04:00
parent 329019b34e
commit 0fd10396f4
3 changed files with 46 additions and 21 deletions
@@ -23,11 +23,6 @@ export default async function updateUserById(
response: "Username invalid.",
status: 400,
};
if (data.newPassword && data.newPassword?.length < 8)
return {
response: "Password must be at least 8 characters.",
status: 400,
};
// Check email (if enabled)
const checkEmail =
@@ -155,6 +150,37 @@ export default async function updateUserById(
);
}
// Password Settings
if (data.newPassword || data.oldPassword) {
if (!data.oldPassword || !data.newPassword)
return {
response: "Please fill out all the fields.",
status: 400,
};
else if (!user?.password)
return {
response:
"User has no password. Please reset your password from the forgot password page.",
status: 400,
};
else if (!bcrypt.compareSync(data.oldPassword, user.password))
return {
response: "Old password is incorrect.",
status: 400,
};
else if (data.newPassword?.length < 8)
return {
response: "Password must be at least 8 characters.",
status: 400,
};
else if (data.newPassword === data.oldPassword)
return {
response: "New password must be different from the old password.",
status: 400,
};
}
// Other settings / Apply changes
const saltRounds = 10;