Skip to content

Commit

Permalink
Added route for updating password
Browse files Browse the repository at this point in the history
  • Loading branch information
A-nirvana committed Apr 7, 2024
1 parent aca556f commit 1121bc5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
36 changes: 35 additions & 1 deletion controllers/authControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,38 @@ const forgotPassword = async (req: Request, res: Response, next: NextFunction) =
}
}

export { signup, login, resetPassword, forgotPassword };
const updatePassword = async (req: Request, res: Response, next: NextFunction) => {
try {
const { email, currentPassword } = req.body;
const user = await UserModel.UserSchema.findOne({ email });
if (!user) {
return res.status(httpStatus.NOT_FOUND).json({
message: "User not found. Please try again!"
})
}
if(currentPassword && !await bcrypt.compare(currentPassword, user.password)) {
return res.status(httpStatus.FORBIDDEN).json({
message: "Current password is incorrect"
})
}
if(req.body.password) {
user.password = req.body.password;
}
else {
return res.status(httpStatus.BAD_REQUEST).json({
message: "Password field is empty"
})
}
await user.save();
return res.status(httpStatus.OK).json({
user: user,
message: "Password updated successfully"
})
}
catch (err) {
next(err);
return;
}
}

export { signup, login, resetPassword, forgotPassword, updatePassword};
1 change: 1 addition & 0 deletions routes/authRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ router.post("/login", authControllers.login);
// router.get("/logout", authControllers.logout);
router.post('/forgotPassword', authControllers.forgotPassword)
router.post('/resetPassword/:id/:resetToken', authControllers.resetPassword)
router.post('/updatePassword', authControllers.updatePassword)

export {router}

0 comments on commit 1121bc5

Please sign in to comment.