0
我目前正在尝试向我的节点API添加一些身份验证。使用await时未定义对象
现在我正在使用PassportJS(对于这个非常新,所以对不起我的不称职者)。
我试图添加本地策略,并检查用户的密码是合法时,在洛:Error: ReferenceError: user is not defined
:
// Local Strategy
passport.use(
new LocalStrategy(async (username, password, done) => {
try {
// Find user by username
const user = await User.findOne({ username })
// No user found
if (!user) {
return done(null, false)
}
console.log('user', user) // Getting output
// Check if password correct
const isMatch = await user.isValidPassword(password)
// Handle if password is not correct
if (!isMatch) {
return done(null, false)
}
// Return user
done(null, user)
} catch (err) {
done(err, false)
}
})
)
东西我已经注意到使用await
上const isMatch = await user.isValidPassword(password)
当邮递员说的是。当我删除await
它工作正常,但我可以键入错误的密码,但我仍然可以登录。我可以在console.log中看到我的user
对象。
{
"username": "martinnord3",
"password": "this_is_the_wrong_password"
}
这里的isValidPassword
功能:
UserSchema.methods.isValidPassword = async function(newPassword) {
try {
return await bcrypt.compare(newPassword, user.password)
} catch (err) {
throw new Error(err)
}
}
我想有一些明显的我失踪,但我不能设法解决这个问题。
感谢您花时间阅读本文!
'user.isValidPassword(password)'返回什么?尽量不要等待。 – Firanolfind
@Firanolfind您好!你的回报是什么意思? :o –