bcryptjs经过2次加密就会匹配失败

因为bcrypt安装一直在gyp..所以我用的是bcryptjs
我将用户注册输入的密码,在前台进行了一次加密,后台接收到以后又进行了加密,然后就会导致用户登录密码不匹配。。。是我的写法错了呢,还是本来就是如此?

这是前台加密逻辑

      bcrypt.genSalt(10, (err, salt) => {
        if (err) {
          console.log(err)
        } else {
          bcrypt.hash(this.password, salt, (err, hash) => {
            if (err) {
              console.log(err)
            } else {
              this.$http
              .post(config.dev.host + '/user/login', {
                username: this.username,
                password: hash
              })
              .then((data) => {
                console.log(data)
              }, (err) => {
                console.log(err)
              })
            }
          })
        }

这是后台UserSchema的方法

User.pre('save', function (next) {
  if (!this.isNew) {
    this.meta.updateAt = Date.now();
  }
  var user = this;
  bcrypt.genSalt(10, function (err, salt) {
    if (err) {
      next(err);
    } else {
      bcrypt.hash(user.password, salt, function (err, hash) {
        if (err) {
          next(err);
        } else {
          console.log(user.username + ' ' + user.password + ' ' + hash);
          user.password = hash;
          next();
        }
      })
    }
  })
});

User.methods = {
  comparePassword: function (_password, cb) {
    var user = this;
    bcrypt.compare(_password, user.password, function (err, isMatched) {
      console.log(_password)
      console.log(user.password)
      if (err) {
        cb(err);
      } else {
        cb(null, isMatched);
      }
    })
  }
}
阅读 3.4k
1 个回答

前台不需要加密。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题