Created
March 27, 2024 21:54
-
-
Save antonkalik/7d7048f51a4c896f5093b1fe8d39e3f2 to your computer and use it in GitHub Desktop.
Reset Password Controller
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bcrypt from 'bcrypt'; | |
import { Request, Response } from 'express'; | |
import { TokenService } from 'src/services/TokenService'; | |
import { UserModel } from 'src/models/UserModel'; | |
import type { User } from 'src/@types'; | |
export const resetPasswordController = async (req: Request, res: Response) => { | |
try { | |
const token = req.params.token; | |
if (!token) { | |
return res.sendStatus(400); | |
} | |
const userData = await TokenService.verify<{ id: number }>(token); | |
const user = await UserModel.findOneById<User>(userData.id); | |
if (!user) { | |
return res.sendStatus(400); | |
} | |
const newPassword = req.body.password; | |
if (!newPassword) { | |
return res.sendStatus(400); | |
} | |
const hashedPassword = await bcrypt.hash(newPassword, 10); | |
await UserModel.updateById(user.id, { password: hashedPassword, passwordResetToken: null }); | |
return res.sendStatus(200); | |
} catch (error) { | |
const errors = ['jwt malformed', 'TokenExpiredError', 'invalid token']; | |
if (errors.includes(error.message)) { | |
return res.sendStatus(400); | |
} | |
return res.sendStatus(500); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment