Skip to content

Commit

Permalink
add: reset password handler controller
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeEventHorizon committed Nov 20, 2023
1 parent dd37db6 commit 93a97ce
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/controller/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Request, Response } from "express";
import {
CreateUserInput,
ForgotPasswordInput,
ResetPasswordInput,
VerifyUserInput,
} from "../schema/user.schema";
import {
Expand Down Expand Up @@ -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<ResetPasswordInput["params"], {}, ResetPasswordInput["body"]>,
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");
}

0 comments on commit 93a97ce

Please sign in to comment.