Advertisement
Guest User

Untitled

a guest
Oct 15th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. router.post('/reset-password', async (req, res) => {
  2.   const generatePassword = () =>
  3.     Math.random()
  4.       .toString(36)
  5.       .toUpperCase()
  6.       .slice(2);
  7.  
  8.   try {
  9.     const user = await User.findOne({ email: req.body.email });
  10.     if (!user) {
  11.       return res.status(422).json({ errors: { email: 'Incorrect email' } });
  12.     }
  13.     const newPassword = generatePassword();
  14.     const salt = await bcrypt.genSalt();
  15.     const hash = await bcrypt.hash(newPassword, salt);
  16.     const updatedUser = await user.update({ password: hash });
  17.  
  18.     const subject = 'Reset password';
  19.     const html = `Here is your new password: ${newPassword} \nWe strongly recommend to change it after first login.`;
  20.     sendEmail({
  21.       from: `"Lovely support" <${mail.auth.user}>`,
  22.       to: user.email,
  23.       subject,
  24.       html
  25.     });
  26.     return res.json(updatedUser);
  27.   } catch (err) {
  28.     throw err;
  29.   }
  30. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement