#151 added try blocks to passwordRecovery.ts

parent 3132fbbd
......@@ -13,6 +13,7 @@ const dataSource = myDataSource;
/**Make requiest to init recovery process */
router.post ('/', async (req:Request, res:Response):Promise<void |Response>=>{
try{
const {email} = req.body
console.log("emeil", email)
const user = await dataSource
......@@ -25,7 +26,6 @@ router.post ('/', async (req:Request, res:Response):Promise<void |Response>=>{
console.log('user ', user)
if (!user) return res.status(404).send({message:'user not found'})
const token = nanoid();
try{
const passwordRecovery = new PasswordRecovery()
passwordRecovery.user= user;
passwordRecovery.token=token;
......@@ -50,6 +50,7 @@ router.post ('/', async (req:Request, res:Response):Promise<void |Response>=>{
/**reset token in password recovery */
router.get('/', async(req: Request, res: Response):Promise<Response|void>=>{
try{
const token = req.query.token;
if(!token) return res.status(401).send({Message:'token not exists'})
const passwordRecovery = await dataSource
......@@ -62,16 +63,16 @@ router.get('/', async(req: Request, res: Response):Promise<Response|void>=>{
if(!passwordRecovery || !passwordRecovery.enabled) return res.status(404).send({message:"Token is not valid"})
res.send(passwordRecovery)
passwordRecovery.enabled=false;
try{
await passwordRecovery.save();
} catch(e){
console.log(e)
}
} catch(e) {
return res.status(502).send({message:(e as Error).message})
}
})
/**change password */
router.patch('/:id/change-password', async (req: Request, res: Response):Promise<Response|void>=>{
try{
const user = await dataSource
.getRepository(User)
.findOneBy({id:req.params.id})
......@@ -79,7 +80,6 @@ router.patch('/:id/change-password', async (req: Request, res: Response):Promise
const salt = await bcrypt.genSalt(SALT_WORK_FACTOR);
const newPassword:string = await bcrypt.hash(req.body.password, salt);
user.password = newPassword
try{
await user.save()
res.send({message:"Password saved"})
} catch (e){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment