首页 > 解决方案 > 使用 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

标签: javascriptnode.js

解决方案


电子邮件中发送的代码应存储在表格或集合中。因此,代码将根据电子邮件和代码的到期时间存储。因此,在代码将通过电子邮件传递的下一阶段,您可以检查此(输入的)电子邮件是否应在此(代码的到期时间)时间范围内属于此(输入的)代码,因此如果满足条件,则请求的用户可以重置/更改密码。


推荐阅读