gpt4 book ai didi

passport.js - 更新 nestjs/graphql 后不通过标题

转载 作者:行者123 更新时间:2023-12-04 11:45:39 25 4
gpt4 key购买 nike

我即将将我的项目依赖项更新到下一个主要版本,但我无法让 nestjs/graphql + nestjs/passport 工作。
看起来请求 header 没有通过 apollo 服务器传递。每次当 Passport 尝试从标题中提取不记名 token 时,我都会收到以下堆栈跟踪的异常:

TypeError: Cannot read property 'headers' of undefined,    at JwtStrategy._jwtFromRequest (/Users/wowa/workspace/foxcms-backend/node_modules/passport-jwt/lib/extract_jwt.js:58:21),    at JwtStrategy.authenticate (/Users/wowa/workspace/foxcms-backend/node_modules/passport-jwt/lib/strategy.js:93:22),    at attempt (/Users/wowa/workspace/foxcms-backend/node_modules/passport/lib/middleware/authenticate.js:361:16)",    at authenticate (/Users/wowa/workspace/foxcms-backend/node_modules/passport/lib/middleware/authenticate.js:362:7)",    at Promise (/Users/wowa/workspace/foxcms-backend/node_modules/@nestjs/passport/dist/auth.guard.js:77:3)",    at new Promise ()",    at /Users/wowa/workspace/foxcms-backend/node_modules/@nestjs/passport/dist/auth.guard.js:69:83",    at MixinAuthGuard. (/Users/wowa/workspace/foxcms-backend/node_modules/@nestjs/passport/dist/auth.guard.js:44:36)",    at Generator.next ()",    at /Users/wowa/workspace/foxcms-backend/node_modules/@nestjs/passport/dist/auth.guard.js:19:71"

This is how my app.module looks like:



@Module({
imports: [
GraphQLModule.forRoot({
typePaths: ['./src/**/*.graphql'],
}),
UserModule,
ContentTypeModule,
PrismaModule,
ProjectModule,
AuthModule,
],
})
export class AppModule implements NestModule {
constructor(
private readonly graphQLFactory: GraphQLFactory,
@Inject('PrismaBinding') private prismaBinding,
) {}
configure(consumer: MiddlewareConsumer) {}
}

我只是想在我在 github 上打开一个问题之前在这里问一下。任何人的想法什么是错的?

最佳答案

您可以使用此表单管理对象请求:

GraphQLModule.forRoot({
typePaths: ['./**/*.graphql'],
installSubscriptionHandlers: true,
context: (({ req }) => {
return { request: req }
}),
},
并创建您自己的守卫:
export class CatsGuard implements CanActivate {
constructor(readonly jwtService: JwtService/*, readonly userService: UsersService*/) {}
canActivate(context: ExecutionContext): boolean {
const ctx = GqlExecutionContext.create(context);
const request = ctx.getContext().request;
const Authorization = request.get('Authorization');

if (Authorization) {
const token = Authorization.replace('Bearer ', '');
const { userId } = this.jwtService.verify(token) as { userId: string };
return !!userId;
}
}
}

关于passport.js - 更新 nestjs/graphql 后不通过标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52126271/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com