Skip to content

Commit

Permalink
Merge branch 'refs/heads/backend'
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] committed Jul 8, 2024
2 parents 2c0a45e + 409351e commit 68a5dc0
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 0 deletions.
20 changes: 20 additions & 0 deletions backend/dist/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions backend/dist/routes/user.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
16 changes: 16 additions & 0 deletions backend/dist/services/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
24 changes: 24 additions & 0 deletions backend/src/controllers/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 2 additions & 0 deletions backend/src/routes/user.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
20 changes: 20 additions & 0 deletions backend/src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

0 comments on commit 68a5dc0

Please sign in to comment.