简体   繁体   English

让 HTTPs 请求等到密码被加密

[英]Make the HTTPs request wait till password is encrypted

So i need to create a post request that gets login data(name, email, password) from the user, runs validations, encrypts password, then stores data.所以我需要创建一个发布请求,从用户那里获取登录数据(名称,email,密码),运行验证,加密密码,然后存储数据。 The problem is that the encryption function takes time to run, and the variable is still not populated by the time i use it.问题是加密 function 需要时间来运行,并且在我使用它时变量仍然没有填充。 Tried using another promise-await inside but didn't work.尝试在内部使用另一个 promise-await 但没有用。 How do i wait till encryptedPass is not null?我如何等到 encryptedPass 不是 null?

// Bcrypt import, initialize number of rounds of salting
saltRounds = 10;

router.post('/user/create', bodyPraser.json(), async (req, res) => {

        // Some code here that runs validations

        // Encrypting password 
        var passwordToEncrypt = req.body.password;
        var encryptedPass;

        // MongoDB model to store data
        const encryptedData = new Model({
            fullname: req.body.fullname,
            email: req.body.email,
            password: encryptedPass
        });
    
        // Salting function
        bcrypt.genSalt(saltRounds, function (err, salt) {
            // Hashing function
            bcrypt.hash(passwordToEncrypt, salt, function (err, hash) {
                // Store hash in database here
                encryptedPass = hash;
            });
        });


        // Save, and store data. Sedn success.
        const dataToSave = await encryptedData.save(); // The password is still null at this point
        res.status(200).json(dataToSave);
        console.log("Data saved");
    }

    catch (error) {
        res.status(400).json({ message: error.message });
        console.log("Data not saved!");
    }
})

Here's the modified code.这是修改后的代码。 I just put everything under your salt function, because everything depends on salt.我只是把所有东西都放在你的盐 function 下,因为一切都取决于盐。

// Bcrypt import, initialize number of rounds of salting
saltRounds = 10;

router.post('/user/create', bodyPraser.json(), async (req, res) => {

        // Some code here that runs validations
        
        // Salting function
        bcrypt.genSalt(saltRounds, function (err, salt) {
            // Hashing function
            bcrypt.hash(req.body.password, salt, function (err, hash) {
                if(err){
                    res.status(400).json({message: 'Something went wrong'});
                } else {
                    // MongoDB model to store data
                    const encryptedData = new Model({
                        fullname: req.body.fullname,
                        email: req.body.email,
                        password: hash
                    });
                    // Save, and store data. Sedn success.
                    const dataToSave = await encryptedData.save();
                    res.status(200).json(dataToSave);
                }
            }
            });
        });
    }

    catch (error) {
        res.status(400).json({ message: error.message });
        console.log("Data not saved!");
    }
})

You need to store data in database at the time once the password is hashed and then do it you are doing it before the password is hashed and you are getting the wrong results,一旦密码被散列,您需要将数据存储在数据库中,然后在密码被散列之前执行它并且您得到错误的结果,

PTR: Also avoid using var instead use let because it's a bad practice PTR:也避免使用 var 而不是使用 let 因为这是一种不好的做法

Do this instead改为这样做

// Bcrypt import, initialize number of rounds of salting
saltRounds = 10;

router.post('/user/create', bodyPraser.json(), async (req, res) => {
    try {
        // Some code here that runs validations

        // Encrypting password 
        let passwordToEncrypt = req.body.password;
        let encryptedPass;
        let encryptedData

        // MongoDB model to store data


        // Salting function
        bcrypt.genSalt(saltRounds, function (err, salt) {
            if (err) {
                return res
                    .status(400)
                    .json({
                        message: 'Something went wrong'
                    });
            } else {
                // Hashing function
                bcrypt.hash(passwordToEncrypt, salt, function (err, hash) {
                    if (err) {
                        return res
                            .status(400)
                            .json({
                                message: 'Something went wrong'
                            });
                    } else {
                        // Store hash in database here
                        encryptedPass = hash;
                        encryptedData = new Model({
                            fullname: req.body.fullname,
                            email: req.body.email,
                            password: encryptedPass
                        });
                    }

                });
            }

        });

        // Save, and store data. Sedn success.
        const dataToSave = await encryptedData.save(); // The password is still null at this point
        console.log("Data saved");
        return res
            .status(200)
            .json(dataToSave);

    }
    catch (error) {
        console.log("Data not saved!");
        return res
            .status(400)
            .json({
                message: error.message
            });

    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM