Typescript 当令牌添加到标头时,不间断查询显示
当我使用graphql将令牌添加到nestjs应用程序的头中时,它会不断重复控制台中的select查询 这是我的jwt中间件 应用程序模块Typescript 当令牌添加到标头时,不间断查询显示,typescript,graphql,nestjs,Typescript,Graphql,Nestjs,当我使用graphql将令牌添加到nestjs应用程序的头中时,它会不断重复控制台中的select查询 这是我的jwt中间件 应用程序模块 export class JwtMiddleware implements NestMiddleware { constructor( private readonly jwtService: JwtService, private readonly userService: UsersService, ) {} async us
export class JwtMiddleware implements NestMiddleware {
constructor(
private readonly jwtService: JwtService,
private readonly userService: UsersService,
) {}
async use(req: Request, res: Response, next: NextFunction) {
if ('x-jwt' in req.headers) {
const token = req.headers['x-jwt'];
try {
const decoded = this.jwtService.verify(token.toString());
if (typeof decoded === 'object' && decoded.hasOwnProperty('id')) {
const user = await this.userService.findById(decoded['id']);
req['user'] = user;
}
} catch (e) {}
}
next();
}
}
export class AppModule implements NestModule {
configure(consumer: MiddlewareConsumer) {
consumer.apply(JwtMiddleware).forRoutes({
path: '/graphql',
method: RequestMethod.POST,
});
}
}