Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Header are not passed through after updating nestjs/graphql

I'm about to update my project dependencies to the next major versions but i can't get nestjs/graphql + nestjs/passport to work. It looks like the request header is not passed through apollo server. Everytime when passport tries to extract the bearer token from the header i get an exception with the following stacktrace:

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) {}
}

I just wanted to ask here before i open an issue on github. Anyone a idea whats wrong?

like image 215
w0wka91 Avatar asked Sep 01 '18 08:09

w0wka91


People also ask

What is resolver in nestjs?

Resolvers provide the instructions for turning a GraphQL operation (a query, mutation, or subscription) into data. They return the same shape of data we specify in our schema -- either synchronously or as a promise that resolves to a result of that shape. Typically, you create a resolver map manually.


1 Answers

You can manage the object request with this form:

GraphQLModule.forRoot({
  typePaths: ['./**/*.graphql'],
  installSubscriptionHandlers: true,
  context: (({ req }) => {
     return { request: req }
  }),
},

And create your own Guard:

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;
    }
  }
}
like image 149
cethap Avatar answered Oct 09 '22 01:10

cethap