javascript - 使用 RESTful API 重置密码
问题描述
所以我正在尝试使用restful API创建一个重置页面。
我在互联网上没有找到太多信息,我可能没有使用最好的方法。
我要做的是在用户的电子邮件中发送一个代码,然后在用户输入代码后决定他是否可以更新通行证。我似乎找不到将第一个请求生成的代码值传递给第二个请求以检查其是否正确的方法。
任何帮助表示赞赏。
提前致谢!
//ForgotPassword?
app.get('/forgotpassword/:username', function (_req, res) {
var seq = (Math.floor(Math.random() * 10000) + 10000).toString().substring(1);
console.log(seq);
mysqlConnection.connect(function () {
mysqlConnection.query('SELECT Email from Customer Where Username = ?', [_req.params.username], (err, results, _fields) => {
if (!err){
console.log(results[0].Email);
var mailOptions = {
from: 'myemail',
to: results[0].Email,
subject: "Password Reset Verification",
text: "If you did not request this, please ignore this email and your password will remain unchanged. CODE: " + seq,
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
//Handle error here
res.send('Please try again!');
} else {
console.log('Email sent: ' + info.response);
res.send('Sent!');
}
})
}
else
console.log(err);
})
})
});
//CodeRandomCodeFromResetPass
app.get('/reset', function (req, res) {
var code;
//if code typed is = to seq sent in the email
res.send("The code is correct");
});```
//then I'll create a post request that updates the password username from the first get request
解决方案
电子邮件中发送的代码应存储在表格或集合中。因此,代码将根据电子邮件和代码的到期时间存储。因此,在代码将通过电子邮件传递的下一阶段,您可以检查此(输入的)电子邮件是否应在此(代码的到期时间)时间范围内属于此(输入的)代码,因此如果满足条件,则请求的用户可以重置/更改密码。
推荐阅读
- python - 如何在 O(1) 中的 numpy 中获得反向映射?
- json - SQL Server 2017 的 Json 输出格式
- function - SSRS 函数需要从附加数据集中读取字段
- java - else 有问题(如果没有理由,程序跳转到 else)
- angular - 选择器返回 404
- javascript - 在每次迭代结束时无条件执行的 RxJs 运算符
- javascript - 离子确认警报:在继续之前等待点击警报
- java - 如何在 Inteliij IDEA 中使用由“数据库”实用程序连接的数据库?
- javascript - 浏览器使用 JS socket.io 连接多个套接字
- r - rbind() 返回矩阵而不是数据帧,arrange() 不安排时间