Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- router.post('/reset-password', async (req, res) => {
- const generatePassword = () =>
- Math.random()
- .toString(36)
- .toUpperCase()
- .slice(2);
- try {
- const user = await User.findOne({ email: req.body.email });
- if (!user) {
- return res.status(422).json({ errors: { email: 'Incorrect email' } });
- }
- const newPassword = generatePassword();
- const salt = await bcrypt.genSalt();
- const hash = await bcrypt.hash(newPassword, salt);
- const updatedUser = await user.update({ password: hash });
- const subject = 'Reset password';
- const html = `Here is your new password: ${newPassword} \nWe strongly recommend to change it after first login.`;
- sendEmail({
- from: `"Lovely support" <${mail.auth.user}>`,
- to: user.email,
- subject,
- html
- });
- return res.json(updatedUser);
- } catch (err) {
- throw err;
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement