diff --git a/Server/controllers/authController.js b/Server/controllers/authController.js index 198d3d382..635983a68 100644 --- a/Server/controllers/authController.js +++ b/Server/controllers/authController.js @@ -7,7 +7,7 @@ const { recoveryValidation, recoveryTokenValidation, newPasswordValidation, - userDeleteValidation + deleteUserParamValidation } = require("../validation/joi"); const logger = require("../utils/logger"); require("dotenv").config(); @@ -248,16 +248,16 @@ const resetPasswordController = async (req, res, next) => { const deleteUserController = async (req, res, next) => { try { // Validate user - await userDeleteValidation.validateAsync(req.params.userId); + await deleteUserParamValidation.validateAsync(req.body); // Check if the user exists - const user = await req.db.getUserById(req.params.userId); + const user = await req.db.getUserByEmail(req); if (!user) { throw new Error(errorMessages.DB_USER_NOT_FOUND); } // 1. Find all the monitors associated with the user id - const monitors = await req.db.getMonitorsByUserId(req.params.userId); + const monitors = await req.db.getMonitorsByUserId(req); // 2. Delete jobs associated with each monitor for (const monitor of monitors) { @@ -275,10 +275,10 @@ const deleteUserController = async (req, res, next) => { } // 5. Delete each monitor - await req.db.deleteMonitorsByUserId(req.params.userId); + await req.db.deleteMonitorsByUserId(user._id); // 6. Delete the user by id - await req.db.deleteUserById(req.params.userId); + await req.db.deleteUser(req); return res.status(200).json({ success: true, diff --git a/Server/routes/authRoute.js b/Server/routes/authRoute.js index e383d4457..3e3057082 100644 --- a/Server/routes/authRoute.js +++ b/Server/routes/authRoute.js @@ -15,7 +15,7 @@ const { router.post("/register", registerController); router.post("/login", loginController); router.post("/user/:userId", verifyJWT, userEditController); -router.delete("/delete/:userId", deleteUserController); +router.delete("/user/:userId", deleteUserController); //Recovery routes router.post("/recovery/request", recoveryRequestController); diff --git a/Server/validation/joi.js b/Server/validation/joi.js index 8379559cd..0aaa20928 100644 --- a/Server/validation/joi.js +++ b/Server/validation/joi.js @@ -43,8 +43,8 @@ const newPasswordValidation = joi.object({ confirm: joi.string(), }); -const userDeleteValidation = joi.object({ - userId: joi.string().required(), +const deleteUserParamValidation = joi.object({ + email: joi.string().email().required(), }); //**************************************** @@ -162,5 +162,5 @@ module.exports = { createCheckBodyValidation, getChecksParamValidation, deleteChecksParamValidation, - userDeleteValidation + deleteUserParamValidation };