保存第一次login历史logging后,ExpressJS应用login失败

我使用PassportJS,MongoDB(Mongoose)和JSON Web令牌实现了一个ExpressJS应用程序。

在成功login路线实施

await user.insertLoginTime(); const token = user.generateJwt(); res.status(200).json({ token }); 

Mongoose模型方法'insertLoginTime'

 userSchema.methods.insertLoginTime = async function() { try { const presentTime = new Date().getTime(); await this.loginHistory.push(presentTime); await this.save(); return; } catch (error) { throw new Error(error); } }; 

首先login成功,“邮递员”收到令牌,但在接下来的尝试中返回无效的密码错误。

删除'insertLoginTime'方法成功login多次尝试。 在“insertLoginTime”实现中是否有任何错误?

我已经解决了这个问题。 这是因为我添加了密码散列到架构的'预'挂钩'保存'。 在保存login历史logging时,哈希密码被再次哈希并保存。 这是删除实际密码散列,并导致下一次尝试失败。