#151 added try blocks to passwordRecovery.ts

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