我使用JSON Web令牌和Passport-jwt软件包。
JobDispature
和内部中间件
const payload = {
id: this.id,
name: this.name,
email: this.email,
avatar: this.avatar,
isAdmin: this.isAdmin
};
JWT.sign( payload, keys.secretOrKey, {
algorithm: 'HS256',
expiresIn: this.expiresIn,
jwtid: await this.getJwtId()
});
我可以跳过 User.findById 并仅使用 jwt_payload 吗?
类似的东西:
module.exports = ( passport ) => {
passport.use(
new JwtStrategy( options, async ( jwt_payload, done ) => {
User.findById( jwt_payload.id ).then( ( user ) => {
if ( user ) {
return done( null, user );
} else {
return done( null, false );
}
});
})
);
};