From 6bf9b83f7e2daca5534a7ec7a8fd34a48879c443 Mon Sep 17 00:00:00 2001 From: patrickjieraldjuan Date: Sun, 28 Jan 2024 12:41:32 +0800 Subject: [PATCH] Updated controller for resetPassword to check if uqual to old password --- controllers/userControllers.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/controllers/userControllers.js b/controllers/userControllers.js index d38542e..a6e1d11 100644 --- a/controllers/userControllers.js +++ b/controllers/userControllers.js @@ -146,18 +146,18 @@ module.exports.resetPassword = async (req, res) => { // Get user ID from the JWT token passed in the authorization headers const userId = req.user.id; - // Get the old and new passwords from the request body - const { oldPassword, newPassword } = req.body; + // Get the new password from the request body + const { newPassword } = req.body; - // Fetch the user from the database - const user = await User.findById(userId); + User.findOne({email : req.user.email}).then(result => - // Check if the old password is equal to the new password - const isOldPasswordEqual = await bcrypt.compare(oldPassword, user.password); + const isPasswordEqualtoOld = bcrypt.compareSync(newPassword, result.password); - if (isOldPasswordEqual) { - return res.status(400).json({ error: "Old password should not be equal to the new password." }); - } + if(isPasswordEqualtoOld){ + + return response.send({message: "New password should not be same with old password"}); + + } // Hash the new password const hashedPassword = await bcrypt.hash(newPassword, 10);