不是有效的BCrypt哈希。发生错误

时间:2019-01-01 18:28:25

标签: mysql node.js

我在比较bcrypt的方法时遇到问题。该方法无法正确比较密码。请从这个问题中解决我。比较方法不起作用。bcrypt的比较方法有问题。该方法无法正确比较密码。请从这个问题中解决我。比较方法不起作用。

  app.post('/upload', (req, res) => {

                // hash and save a password
              const pass = bcrypt.hashSync(req.body.password);
              const username = req.body.username;
              console.log(bcrypt.compareSync(req.body.password, pass));
              const sql = "INSERT INTO data ( password, username ) values (?,?)";
              db.query(sql, [pass, username], (err, rows, fields) =>  {
                                       if(!err){
                                                 res.send({
                                                   key: rows
                                                 });
                                               }
                                                 else {
                                                   console.log(err);
                                                 }
                                     });
        })

        app.post('/show', (req, res) => {

          const username = req.body.username;
          const password = req.body.password;

          db.query("SELECT * FROM data WHERE username = ?",[username], function(err, results){
            if (err) {
                    // console.log("error ocurred",error);
                    res.send({
                    "code":400,
                    "failed":"err ocurred"
                    })
                  }else{
                    if(results.length >0){

                    // console.log(bcrypt.compareSync(password, results[0].password));

                      if(bcrypt.compareSync(password, results[0].password)){
                                res.send({
                                "code":200,
                                "success":"login sucessfull"
                                });
                            }
                            else{
                                res.send({
                                "code":204,
                                "success":"Email and password does not match"
                                });
                            }

                    }else{
                          res.send({
                          "code":204,
                          "success":"Email does not exits"
                          });
                          console.log(results.length);
                          }
                  }

          })

        })

0 个答案:

没有答案