From 409351eade8fcc70979816da08b2e3e8988ad591 Mon Sep 17 00:00:00 2001 From: Md-Rubel-Ahmed-Rana Date: Mon, 8 Jul 2024 18:28:49 +0600 Subject: [PATCH] Change password api design done --- backend/dist/controllers/user.controller.js | 20 +++++++++++++++++ backend/dist/routes/user.route.js | 1 + backend/dist/services/user.service.js | 16 ++++++++++++++ backend/src/controllers/user.controller.ts | 24 +++++++++++++++++++++ backend/src/routes/user.route.ts | 2 ++ backend/src/services/user.service.ts | 20 +++++++++++++++++ 6 files changed, 83 insertions(+) diff --git a/backend/dist/controllers/user.controller.js b/backend/dist/controllers/user.controller.js index 2781673..fbb40b6 100644 --- a/backend/dist/controllers/user.controller.js +++ b/backend/dist/controllers/user.controller.js @@ -110,6 +110,26 @@ class Controller extends rootController_1.default { data: null, }); })); + this.changePassword = this.catchAsync((req, res) => __awaiter(this, void 0, void 0, function* () { + const { userId, oldPassword, newPassword } = req.body; + const result = yield user_service_1.UserService.changePassword(userId, oldPassword, newPassword); + if (!result) { + this.apiResponse(res, { + success: false, + statusCode: http_status_1.default.OK, + message: "Your old password was not correct", + data: null, + }); + } + else { + this.apiResponse(res, { + success: true, + statusCode: http_status_1.default.OK, + message: "Your password was changed", + data: null, + }); + } + })); this.logout = this.catchAsync((req, res) => __awaiter(this, void 0, void 0, function* () { res.clearCookie("tmAccessToken", { httpOnly: true, diff --git a/backend/dist/routes/user.route.js b/backend/dist/routes/user.route.js index edf0d4b..2d52858 100644 --- a/backend/dist/routes/user.route.js +++ b/backend/dist/routes/user.route.js @@ -18,4 +18,5 @@ router.post("/login", (0, validateRequest_1.default)(user_validation_1.UserValid router.delete("/logout", user_controller_1.UserController.logout); router.post("/forget-password", user_controller_1.UserController.forgetPassword); router.post("/reset-password", user_controller_1.UserController.resetPassword); +router.post("/change-password", user_controller_1.UserController.changePassword); exports.UserRoutes = router; diff --git a/backend/dist/services/user.service.js b/backend/dist/services/user.service.js index ad0d150..2e39514 100644 --- a/backend/dist/services/user.service.js +++ b/backend/dist/services/user.service.js @@ -113,5 +113,21 @@ class Service { }); }); } + changePassword(userId, oldPassword, newPassword) { + return __awaiter(this, void 0, void 0, function* () { + const user = yield user_model_1.default.findById(userId); + const isPassMatch = yield bcrypt_1.default.compare(oldPassword, user === null || user === void 0 ? void 0 : user.password); + if (!isPassMatch) { + return false; + } + else { + const hashedPassword = yield bcrypt_1.default.hash(newPassword, 12); + yield user_model_1.default.findByIdAndUpdate(userId, { + $set: { password: hashedPassword }, + }); + return true; + } + }); + } } exports.UserService = new Service(); diff --git a/backend/src/controllers/user.controller.ts b/backend/src/controllers/user.controller.ts index 1fc9cb5..325d986 100644 --- a/backend/src/controllers/user.controller.ts +++ b/backend/src/controllers/user.controller.ts @@ -102,6 +102,30 @@ class Controller extends RootController { }); }); + changePassword = this.catchAsync(async (req: Request, res: Response) => { + const { userId, oldPassword, newPassword } = req.body; + const result = await UserService.changePassword( + userId, + oldPassword, + newPassword + ); + if (!result) { + this.apiResponse(res, { + success: false, + statusCode: httpStatus.OK, + message: "Your old password was not correct", + data: null, + }); + } else { + this.apiResponse(res, { + success: true, + statusCode: httpStatus.OK, + message: "Your password was changed", + data: null, + }); + } + }); + logout = this.catchAsync(async (req: Request, res: Response) => { res.clearCookie("tmAccessToken", { httpOnly: true, diff --git a/backend/src/routes/user.route.ts b/backend/src/routes/user.route.ts index 16d1c36..613dbb3 100644 --- a/backend/src/routes/user.route.ts +++ b/backend/src/routes/user.route.ts @@ -34,4 +34,6 @@ router.post("/forget-password", UserController.forgetPassword); router.post("/reset-password", UserController.resetPassword); +router.post("/change-password", UserController.changePassword); + export const UserRoutes = router; diff --git a/backend/src/services/user.service.ts b/backend/src/services/user.service.ts index cef9a64..90a1872 100644 --- a/backend/src/services/user.service.ts +++ b/backend/src/services/user.service.ts @@ -122,6 +122,26 @@ class Service { $set: { password: hashedPassword }, }); } + async changePassword( + userId: string, + oldPassword: string, + newPassword: string + ) { + const user = await User.findById(userId); + const isPassMatch = await bcrypt.compare( + oldPassword, + user?.password as string + ); + if (!isPassMatch) { + return false; + } else { + const hashedPassword = await bcrypt.hash(newPassword, 12); + await User.findByIdAndUpdate(userId, { + $set: { password: hashedPassword }, + }); + return true; + } + } } export const UserService = new Service();