【问题标题】:Mongoose compare BCRYPT passwords not filledMongoose 比较 BCRYPT 密码未填写
【发布时间】:2019-06-28 04:25:42
【问题描述】:

我正在尝试使用打字稿在猫鼬模型中添加 bcrypt。 我以this link 为例。

但我的项目使用的是打字稿,所以我不能完全使用相同的代码。我不明白他们从哪里获得密码来与用户发送密码进行匹配。

当我比较密码时,似乎其中一个密码未定义。 以下是我的代码,不胜感激。

 PersonTestSchema.pre<PersonTestModel>('save', function (next) {
  const user = this;
  if (this.password && this.password.length > 4) {
    bcrypt.genSalt(10, function (err, salt) {
      bcrypt.hash(user.password, salt, (err, hash) => {
        user.password = hash;
        next();
      });
    });
  } else {
    next();
  }
});

PersonTestSchema.methods.verifyPassword = function (candidatePassword: string) {
  const user = this;
  return bcrypt.compareSync(candidatePassword, user.password);

当我记录候选密码时,它会被填满,但如果我记录 user.password 它是空的。 这在我的代码中似乎是合乎逻辑的,但我不明白他们在示例代码中从哪里得到它。

【问题讨论】:

    标签: typescript express mongoose bcrypt


    【解决方案1】:

    我想通了。结果我不得不修改他们的护照配置文件版本,最好的方法是从那里传递数据库中的密码

    【讨论】:

      猜你喜欢
      • 2017-10-31
      • 2012-10-31
      • 1970-01-01
      • 2019-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-11
      相关资源
      最近更新 更多