7

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?

w0wka91
  • 189
  • 2
  • 9
  • Hello @w0wka91 I am also facing this error. Can you tell me how did you recover this ? The answer below writeen by cethap is not even solving my problem. – Shamim Dec 19 '20 at 06:52

2 Answers2

8

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;
    }
  }
}
Demtax
  • 3
  • 2
cethap
  • 101
  • 4
2

The provided AuthGuard from the passport module is currently not working with the graphql module. https://github.com/nestjs/graphql/issues/48

w0wka91
  • 189
  • 2
  • 9