Bcrypt比较总是返回false,哈希长度可以

时间:2019-12-15 10:41:53

标签: javascript node.js electron sequelize.js bcrypt

从昨天开始我一直在谷歌搜索,但是还没有解决方案。我正在尝试使用 import org.springframework.transaction.interceptor.TransactionAspectSupport; public int aBC(XYZ xyz) { try { //transaction 1 //transaction 2 } catch (Exception e) { TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); return -1; } return 1; } 实现非常简单的登录。当前,无论我提供什么密码(也尝试过硬编码),bcrypt方法都将返回false。我究竟做错了什么?我也已经在控制台中检查了哈希值,它是60个字符(有些堆栈溢出的答案是在某些人的哈希值被截断的情况下寻找的)。我将compare用于ORM。下面的代码:

model.js

sequelize

main.dev.js(只是登录部分)

const bcrypt = require('bcrypt');

// eslint-disable-next-line no-unused-vars
async function hashPassword(teacher, options) {
  if (!teacher.changed('password')) {
    return 0;
  }
  const SALT_FACTOR = 8;
  // eslint-disable-next-line no-param-reassign
  teacher.password = await bcrypt.hash(teacher.password, SALT_FACTOR);
}

module.exports = (sequelize, DataTypes) => {
  const Teacher = sequelize.define(
    'Teacher',
    {
      name: DataTypes.STRING,
      username: {
        type: DataTypes.STRING,
        unique: true,
        allowNull: false
      },
      password: {
        type: DataTypes.STRING,
        allowNull: false
      }
    },
    {
      hooks: {
        beforeSave: hashPassword,
        beforeCreate: hashPassword
      }
    }
  );
  // eslint-disable-next-line func-names
  Teacher.prototype.comparePassword = function(password) {
    // eslint-disable-next-line func-names
    return bcrypt.compare(password, this.password);
  };
  // eslint-disable-next-line no-unused-vars,func-names
  Teacher.associate = function(models) {
    // associations can be defined here
  };

  // create all the defined tables in the specified database.
  sequelize
    .sync()
    .then(() =>
      console.log(
        "users table has been successfully created, if one doesn't exist"
      )
    )
    .catch(error => console.log('This error occured', error));

  return Teacher;
};

0 个答案:

没有答案