Skip to content

Commit

Permalink
Merge pull request #33 from atlp-rwanda/fix-password-update-#187419174
Browse files Browse the repository at this point in the history
fix(profile-edit): fix password not being updated in the database
  • Loading branch information
teerenzo authored Apr 29, 2024
2 parents fb51f5b + 0980a28 commit 5d9a0bf
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
14 changes: 6 additions & 8 deletions src/controllers/userControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ export const createUserController = async (req: Request, res: Response) => {
}
return res.status(201).json({
status: 201,
message: "User successfully created.",
user,
message: "User successfully created."
});
} catch (err: any) {
if (err.name === "UnauthorizedError" && err.message === "User already exists") {
Expand All @@ -99,10 +98,7 @@ export const updatePassword = async (req: Request, res: Response) => {
const { oldPassword, newPassword, confirmPassword } = req.body;
try {
// @ts-ignore
const user = await getUserByEmail(req.user.email);
if (!user) {
return res.status(404).json({ message: "User not found" });
}
const { user } = req;
const isPasswordValid = await comparePasswords(oldPassword, user.password);
if (!isPasswordValid) {
return res.status(400).json({ message: "Old password is incorrect" });
Expand All @@ -117,8 +113,10 @@ export const updatePassword = async (req: Request, res: Response) => {
}

const password = await hashedPassword(newPassword);
await updateUserPassword(user, password);
return res.status(200).json({ message: "Password updated successfully" });
const update = await updateUserPassword(user, password);
if(update){
return res.status(200).json({ message: "Password updated successfully" });
}
} catch (err: any) {
return res.status(500).json({
message: err.message,
Expand Down
5 changes: 2 additions & 3 deletions src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ export const findUserById = async (id: string) => {
}
};
export const updateUserPassword = async (user: User, password: string) => {
user.password = password;
const update = await user.save;
return update;
const update = await User.update({ password: password}, { where: { id: user.id}})
return update
};

0 comments on commit 5d9a0bf

Please sign in to comment.