From 1121bc53d525ed774e1d325ee8384a767d81e3f5 Mon Sep 17 00:00:00 2001 From: A-nirvana Date: Sun, 7 Apr 2024 19:24:11 +0530 Subject: [PATCH 1/3] Added route for updating password --- controllers/authControllers.ts | 36 +++++++++++++++++++++++++++++++++- routes/authRoutes.ts | 1 + 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/controllers/authControllers.ts b/controllers/authControllers.ts index e9b0eb9..d933ee4 100644 --- a/controllers/authControllers.ts +++ b/controllers/authControllers.ts @@ -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}; diff --git a/routes/authRoutes.ts b/routes/authRoutes.ts index a184a82..7dd9521 100644 --- a/routes/authRoutes.ts +++ b/routes/authRoutes.ts @@ -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} \ No newline at end of file From 94a8192cd5e699bda5a069d0b31d0ff12ecbe53e Mon Sep 17 00:00:00 2001 From: A-nirvana Date: Sun, 7 Apr 2024 19:26:14 +0530 Subject: [PATCH 2/3] Revert "Added route for updating password" This reverts commit 1121bc53d525ed774e1d325ee8384a767d81e3f5. --- controllers/authControllers.ts | 36 +--------------------------------- routes/authRoutes.ts | 1 - 2 files changed, 1 insertion(+), 36 deletions(-) diff --git a/controllers/authControllers.ts b/controllers/authControllers.ts index d933ee4..e9b0eb9 100644 --- a/controllers/authControllers.ts +++ b/controllers/authControllers.ts @@ -106,38 +106,4 @@ const forgotPassword = async (req: Request, res: Response, next: NextFunction) = } } -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}; +export { signup, login, resetPassword, forgotPassword }; diff --git a/routes/authRoutes.ts b/routes/authRoutes.ts index 7dd9521..a184a82 100644 --- a/routes/authRoutes.ts +++ b/routes/authRoutes.ts @@ -8,6 +8,5 @@ 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} \ No newline at end of file From 276711298509687c49525e77723ac88d2efd5202 Mon Sep 17 00:00:00 2001 From: A-nirvana Date: Sun, 7 Apr 2024 19:30:45 +0530 Subject: [PATCH 3/3] Added route for updating password --- controllers/authControllers.ts | 38 +++++++++++++++++++++++++++++++++- routes/authRoutes.ts | 1 + 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/controllers/authControllers.ts b/controllers/authControllers.ts index e9b0eb9..86a77df 100644 --- a/controllers/authControllers.ts +++ b/controllers/authControllers.ts @@ -106,4 +106,40 @@ 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) { + return res.status(httpStatus.BAD_REQUEST).json({ + message: "Something went wrong. Please try again!", + error: next(err) + }); + } +} + +export { signup, login, resetPassword, forgotPassword, updatePassword}; diff --git a/routes/authRoutes.ts b/routes/authRoutes.ts index a184a82..7dd9521 100644 --- a/routes/authRoutes.ts +++ b/routes/authRoutes.ts @@ -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} \ No newline at end of file