忘记密码与节点和 sendgride 问题
forgotten password with node and sendgride issue
我试过如下设置忘记密码后端,但似乎不起作用。
exports.forgotPassword = (req, res) => {
const { email } = req.body.email;
User.findOne({ email }, (err, user) => {
if (err || !user) {
return res.status(401).json({
error: 'User with that email does not exist'
});
}
const token = jwt.sign({ _id: user._id }, process.env.JWT_RESET_PASSWORD, { expiresIn: '10m' });
// email
const emailData = {
from: process.env.EMAIL_FROM,
to: email,
subject: `Password reset link`,
html: `
<p>Please use the following link to reset your password:</p>
<p>${process.env.CLIENT_URL}/auth/password/reset/${token}</p>
<hr />
<p>This email may contain sensetive information</p>
`
};
// populating the db > user > resetPasswordLink
return user.updateOne({ resetPasswordLink: token }, (err, success) => {
if (err) {
return res.json({ error: errorHandler(err) });
} else {
sgMail.send(emailData).then(sent => {
return res.json({
message: `Email has been sent to ${email}. Follow the instructions to reset your password. Link expires in 10min.`
});
});
}
});
});
};
在邮递员上测试显示发送不成功和错误
取消继续在邮递员中发送请求,邮递员控制台没有错误。但是,我的终端控制台有这个有趣的响应
如有任何帮助,我将不胜感激。
谢谢。
由于 express 不知道何时从一个函数转到另一个函数,您需要调用传递给这些函数的 next() 参数(在本例中:forgotPasswordValidator 和 forgotPassword)。您可以在这里找到更多相关信息:http://expressjs.com/en/guide/using-middleware.html
路由器:
const validationRules = () => {
return [
check('email')
.notEmpty().withMessage('Please add an email address')
.isEmail().withMessage('Must be a valid email address')
]
}
router.put("/forgot-password", validationRules(), forgotPasswordValidator, forgotPassword);
忘记密码验证器中间件函数:
const { validationResult } = require('express-validator');
exports.forgotPasswordValidator = (req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
} else next()
};
忘记密码功能貌似还不错,如果还有什么问题欢迎大家留言。
我试过如下设置忘记密码后端,但似乎不起作用。
exports.forgotPassword = (req, res) => {
const { email } = req.body.email;
User.findOne({ email }, (err, user) => {
if (err || !user) {
return res.status(401).json({
error: 'User with that email does not exist'
});
}
const token = jwt.sign({ _id: user._id }, process.env.JWT_RESET_PASSWORD, { expiresIn: '10m' });
// email
const emailData = {
from: process.env.EMAIL_FROM,
to: email,
subject: `Password reset link`,
html: `
<p>Please use the following link to reset your password:</p>
<p>${process.env.CLIENT_URL}/auth/password/reset/${token}</p>
<hr />
<p>This email may contain sensetive information</p>
`
};
// populating the db > user > resetPasswordLink
return user.updateOne({ resetPasswordLink: token }, (err, success) => {
if (err) {
return res.json({ error: errorHandler(err) });
} else {
sgMail.send(emailData).then(sent => {
return res.json({
message: `Email has been sent to ${email}. Follow the instructions to reset your password. Link expires in 10min.`
});
});
}
});
});
};
在邮递员上测试显示发送不成功和错误
取消继续在邮递员中发送请求,邮递员控制台没有错误。但是,我的终端控制台有这个有趣的响应
如有任何帮助,我将不胜感激。
谢谢。
由于 express 不知道何时从一个函数转到另一个函数,您需要调用传递给这些函数的 next() 参数(在本例中:forgotPasswordValidator 和 forgotPassword)。您可以在这里找到更多相关信息:http://expressjs.com/en/guide/using-middleware.html
路由器:
const validationRules = () => {
return [
check('email')
.notEmpty().withMessage('Please add an email address')
.isEmail().withMessage('Must be a valid email address')
]
}
router.put("/forgot-password", validationRules(), forgotPasswordValidator, forgotPassword);
忘记密码验证器中间件函数:
const { validationResult } = require('express-validator');
exports.forgotPasswordValidator = (req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() });
} else next()
};
忘记密码功能貌似还不错,如果还有什么问题欢迎大家留言。