这个是我的jwt.strategy.ts
import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor() {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey: 'secretOrKey',
});
}
async validate(payload: any) {
return {
userId: payload.userId,
};
}
}
但是有个小小的困惑,就是我一旦用户登录成功,我在调用别的接口时,怎么获取jwt token中的用户信息呢?
比如我有个getProfile
接口,这个时候,我肯定需要知道存储在jwt token中的userId信息,我记得以前用Koa/jwt的时候,它会自动将jwt中存储的信息放到ctx.state.user
中,但在nest里面似乎没有。
请问下,我怎么才能获取到这个userId呢.....
在request.user里面获取