【发布时间】:2020-11-07 16:22:46
【问题描述】:
我正在使用jsonwebtoken 包来生成 jwt 令牌。
这是我的用户模型
const userSchema = new mongoose.Schema({
username: {
type: String,
required: true,
},
password: {
type: String,
required: true,
}
})
这是为每个用户生成令牌的用户模式方法:
userSchema.methods.generateAuthToken = () => {
var token = jwt.sign({ _id: this._id}, 'shhhhh');
return token
}
但是,在运行这段代码来验证令牌时,在记录有效负载时,我会记录 undefined 而不是有效负载。有人知道为什么吗?
jwt.verify(token, 'shhhhh', function(err, decoded) {
if (err) console.log(err)
console.log(decoded._id) // bar
});
【问题讨论】: