diff --git a/app/controllers/user.controller.js b/app/controllers/user.controller.js index a5c8a97..c3f8661 100644 --- a/app/controllers/user.controller.js +++ b/app/controllers/user.controller.js @@ -92,3 +92,44 @@ exports.deleteUser = async (req, res) => { res.status(500).send({'status':"failed",'message':err.message }); } }; + +// Change User Password +exports.changeAdminUserPassword = async (req, res) => { + try { + const { old_password, new_password, confirm_password } = req.body; + const userId = req.params.id; + + // Validate inputs + if (!old_password || !new_password || !confirm_password) { + return res.status(400).send({ status: "failed", message: "All password fields are required" }); + } + + if (new_password !== confirm_password) { + return res.status(400).send({ status: "failed", message: "New password and confirm password do not match" }); + } + + // Find user + const user = await User.findByPk(userId); + if (!user) { + return res.status(404).send({ status: "failed", message: "User not found" }); + } + + // Verify old password + const isMatch = await bcrypt.compare(old_password, user.password); + if (!isMatch) { + return res.status(400).send({ status: "failed", message: "Old password is incorrect" }); + } + + // Hash and update new password + const hashedPassword = await bcrypt.hash(new_password, 10); + await User.update( + { password: hashedPassword }, + { where: { id: userId } } + ); + + return res.status(200).send({status: "success", message: "Password updated successfully",}); + + } catch (err) { + return res.status(500).send({ status: "failed", message: err.message }); + } +}; diff --git a/app/routes/routes.js b/app/routes/routes.js index aadf8b3..89157d0 100644 --- a/app/routes/routes.js +++ b/app/routes/routes.js @@ -330,6 +330,55 @@ router.put("/admin_users/:id",[verifySignature, verifyToken], adminUserControlle router.delete("/admin_users/:id",[verifySignature, verifyToken], adminUserController.deleteUser); +/** + * @swagger + * /api/admin_users/{id}/change-password: + * put: + * summary: Change Admin User Password + * description: Allows an Admin user to change their password after verifying the old password. + * tags: [Admin Users] + * security: + * - appSignature: [] + * - bearerAuth: [] + * parameters: + * - name: id + * in: path + * required: true + * description: ID of the admin user + * schema: + * type: integer + * requestBody: + * required: true + * content: + * application/json: + * schema: + * type: object + * required: + * - old_password + * - new_password + * - confirm_password + * properties: + * old_password: + * type: string + * example: "OldPassword@123" + * new_password: + * type: string + * example: "NewPassword@123" + * confirm_password: + * type: string + * example: "NewPassword@123" + * responses: + * 200: + * description: Password updated successfully + * 400: + * description: Validation or password mismatch error + * 404: + * description: User not found + * 500: + * description: Internal server error + */ +router.put("/admin_users/:id/change-password",[verifySignature, verifyToken], adminUserController.changeAdminUserPassword); + @@ -804,7 +853,7 @@ router.put("/establishment-users/:id",[verifySignature, verifyToken], establishm * 500: * description: Internal server error */ -router.put("/establishment-users/:id/change-password", establishmentUserController.changeUserPassword); +router.put("/establishment-users/:id/change-password",[verifySignature, verifyToken], establishmentUserController.changeUserPassword); /**