我使用 JSON Web Tokens 和 passport-jwt 包。
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()
});
和内部中间件
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 );
}
});
})
);
};
我可以跳过User.findById并仅使用jwt_payload吗?
像这样的东西:
module.exports = ( passport ) => {
passport.use(
new JwtStrategy( options, ( jwt_payload, done ) => {
return done( null, jwt_payload );
})
);
};