【问题标题】:Bcryptjs always return falseBcryptjs 总是返回 false
【发布时间】:2022-01-02 11:10:17
【问题描述】:

您好,我正在尝试使用 bcriptjs 库来比较加密密码和数据库密码。
我正在使用“比较”方法。

我添加到方法中的值是:

  • 首先,用户给我的密码。 (密码)
  • 二、数据库的加密密码。 (pass_db)

    但总是返回 false
const bcrypt = require('bcryptjs')

const create = (req, res) => {
  const { email, password } = req.body;

  if (password.length < 6) {
    res.send({ msgType: "error", msg: "Contraseña almenos 6 caracteres" });
  } else {
    const hash = bcrypt.hashSync(password, 6);

    db.query(
      "INSERT INTO users (email, password) VALUES (?,?)",
      [email, hash],
      (err) => {
        res.send({ msgType: "success", msg: "Usuario creado correctamente" });
      }
    );
  }
};

//TODO Login dont work always return false
const login = (req, res) => {

  //Password from req.body
  const { email, password } = req.body;

  db.query(
    "SELECT password FROM users WHERE email = ?",
    [email],
    (err, result) => {
      // Password encrypted from database
      pass_db = result[0].password;

      if(err){
        res.send({msgType:'error', msg:'Incorrect Login'})
      }

      if(result.length > 0){
        
        //Compare password from req.body with password encryted from database
        const validate = bcrypt.compareSync(password, pass_db);

        //Always false
        console.log(validate);

        if(validate){
          res.send({msgType:'success', msg: "Correct login" })
        }else{
          res.send({msgType:'error', msg: "Incorrect email or password" });
        }
      }else{
        res.send({msgType:'error', msg: "Incorrect email or password" });
      }
    }
  );
};

我也试过这段代码,但没有成功。

bcrypt.hash(password, 6, function (err, hash) {
        if (err) {
          throw err;
        }

        bcrypt.compare(pass_db, hash, function (err, result) {
          if (err) {
            throw err;
          }
          console.log(result);
        });
      });

希望你能帮忙解决这个问题,谢谢。

【问题讨论】:

  • 如果它的require('bcrytpjs') 错误应该是require('bcryptjs'),除此之外您是否检查过pass_db 和密码是预期值,也许您应该验证它们是否已设置,如果电子邮件未设置会发生什么找到了,服务器崩溃了吗?
  • 您在数据库中的加密/散列密码是否使用 bcrypt 创建并遵循此格式? github.com/kelektiv/node.bcrypt.js#hash-info
  • 最好编辑你的问题以包含它(更好的格式,其他人会立即看到它)
  • @AlwaysLearning 我已经添加了create方法,你可以看到我是如何保存数据的(email, hash)
  • 请澄清您的具体问题或提供其他详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。

标签: javascript node.js reactjs bcryptjs


【解决方案1】:

只要正确检索存储在数据库中的密码,它就可以工作。我将程序简化为不使用数据库(只是一个本地对象来存储散列密码)并且它工作正常。由于我使用的是与您相同的 bcrypt 命令,因此我唯一能想象的是哈希密码没有从数据库正确返回存在问题。尝试在create 期间打印出散列值,并在从数据库检索并确认它们匹配后再次打印。如果pass_db 不完全是它应该是的,那就可以解释为什么你不能成功比较。您确定从db.query 返回的result[0] 是一个具有password 属性的JSON 对象吗?

这是我在不使用 db 的情况下工作的代码的简化版本:

const bcrypt = require('bcryptjs')
const passwords = {};

const create = async (email, password) => {
  if (password.length < 6) {
    console.log("Password must be at least 6 characters");
  } else {
    const hash = bcrypt.hashSync(password, 6);
    passwords[email] = hash;  // Store the hash in the passwords instead of db
  }
};

const login = (email, password) => {
  const pass_db = passwords[email];  // Retrieve hash from passwords instead of db
  if (pass_db) {
    const validate = bcrypt.compareSync(password, pass_db);
    if (validate) {
      console.log('Correct login');
    } else {
      console.log('Incorrect email or password');
    }
  } else {
    console.log('Incorrect email or password');
  }
};

const email = 'abc@mail.com';
const pw = 'testpass12345';
create(email, pw);
console.log('Try the right password');
login(email, pw);
console.log('Try the wrong password');
login(email, 'wrongpw');
console.log('Try the wrong email');
login('unknown@abc.com', 'blah');

运行这个程序会得到以下输出:

Try the right password
Correct login
Try the wrong password
Incorrect email or password
Try the wrong email
Incorrect email or password

【讨论】:

    猜你喜欢
    • 2021-09-29
    • 2020-05-24
    • 1970-01-01
    • 1970-01-01
    • 2016-07-20
    • 2021-11-05
    • 2018-11-05
    • 2019-05-06
    • 2017-04-29
    相关资源
    最近更新 更多