mysql - 无法比较我的数据库密码和输入字段密码

标签 mysql node.js

我输入的密码和数据库密码相同,然后函数也返回 false。这里的比较方法无法正常工作。每次提供正确的密码时,我都会得到错误的返回值。

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


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

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

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

                      if(bcrypt.compareSync(password, rows[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"
                  });
                  }
              }
          });


})

最佳答案

您的数据库中可能有多个用户名,这些用户名与您提供的用户名相匹配,因此它会尝试将其与第一个用户名匹配,但密码将是不同的..

关于mysql - 无法比较我的数据库密码和输入字段密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53995765/

相关文章:

PHP、MYSQL INSERT INTO 变音符号,如 ä、ü、ö

将数据库从 sqlite 更改为 mysql 时,Python manage.py 迁移错误

jquery - 如何从node.js-express获取响应返回ajax

node.js - Google Cloud Datastore 中的 Arrays/ListValues 中的顺序是否保留?

node.js - 为什么 fs.readFile 回调函数下面的代码在回调之前运行?

node.js - 如何找到两个复杂的json对象之间的差异?

mysql - 创建 SQL 命令以从给定表中检索数据

php - 如何使用 CURRENT_TIMESTAMP 在 mysql 中将 Gregorian 格式转换为 Solar 格式

php登录密码_验证

node.js - Skaffold 同步文件但 pod 不刷新