From 93a97ced104ec0e35fe589e2675ad34221a8cffa Mon Sep 17 00:00:00 2001 From: Nikoloz Muladze Date: Mon, 20 Nov 2023 17:56:13 +0400 Subject: [PATCH] add: reset password handler controller --- src/controller/user.controller.ts | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/controller/user.controller.ts b/src/controller/user.controller.ts index de11d73..b425255 100644 --- a/src/controller/user.controller.ts +++ b/src/controller/user.controller.ts @@ -2,6 +2,7 @@ import { Request, Response } from "express"; import { CreateUserInput, ForgotPasswordInput, + ResetPasswordInput, VerifyUserInput, } from "../schema/user.schema"; import { @@ -104,3 +105,29 @@ export async function forgotPasswordHandler( log.debug(`Password reset email sent to ${email}`); return res.send(message); } + +export async function resetPasswordHandler( + req: Request, + res: Response +) { + const { id, passwordResetCode } = req.params; + const { password } = req.body; + + const user = await findUserById(id); + + if ( + !user || + !user.passwordResetCode || + user.passwordResetCode !== passwordResetCode + ) { + return res.status(400).send("Could not reset user password"); + } + + user.passwordResetCode = null; + // we can do this without hashing because we have @pre hook in user model + user.password = password; + + await user.save(); + + return res.send("Successfully updated user password"); +}