使用nodejs的bcrypt问题

时间:2017-11-27 10:27:08

标签: mysql node.js

我已成功加密密码并在注册时存储在我的数据库中。但是当我登录时,我正在比较密码并尝试登录,在登录期间我的sql查询正在下降以读取密码而我得到哈希未定义。请让我知道我哪里出错了。

 router.post('/login', function(req,res) {
 var password = req.body.password;
 var user_name = req.body.user_name;
 var response = {};
bcrypt.compare(password, hash, function(err, res) {
db.query('select user_id,email FROM user where password = ? AND user_name = ? OR email = ?',
[hash, req.body.user_name, req.body.user_name], function (error,rows) {
  if (error) {
    res.json(error)
} else {    
 response.msg = 'Login Success';
    }
  });
 }
});

1 个答案:

答案 0 :(得分:0)

router.post('/login', function(req,res) {
  var password = req.body.password;
  var user_name = req.body.user_name;
  var response = {};
  var hash = db.query('SELECT hash FROM user WHERE user_name = ?', [req.body.user_name]);

  bcrypt.compare(password, hash, function(err, res) {
  db.query('select user_id,email FROM user where password = ? AND user_name = ? OR email = ?',
[hash, req.body.user_name, req.body.user_name], function (error,rows) {
    if (error) {
      res.json(error)
    } else {    
      response.msg = 'Login Success';
    }
  });
 }
});