title: Authorization in Nestjs using graphql
body: <p>I have started to learn Nestjs, express and graphql.
  I encountered a problem while trying to authorize access of user authenticated using jwt token.
  I followed the tutorial for authentication on the <a href="https://docs.nestjs.com/security/authentication#graphql" rel="noreferrer">Nestjs website</a>.
  I am able to get the current user, but when I try implementing the <a href="https://docs.nestjs.com/security/authorization#basic-rbac-implementation" rel="noreferrer">basic role base access control</a>, I am unable to access the current user in the canActivate Method.
  I think it is because the Roles Guard is executed before the Graphql Guard.</p>
  <p>I will post the codes here</p>
  <p>gql-auth.guard.ts</p>
  <pre><code>import { ExecutionContext } from &quot;@nestjs/common&quot;;
  import { GqlExecutionContext } from &quot;@nestjs/graphql&quot;;
  import { AuthGuard } from &quot;@nestjs/passport&quot;;
  
  export class GqlAuthGuard extends AuthGuard(&quot;jwt&quot;) {
      getRequest(context: ExecutionContext) {
          const ctx = GqlExecutionContext.create(context);
          console.log(&quot;gql simple context: &quot;, context);
          console.log(&quot;gqlContext: &quot;, ctx.getContext());
          return ctx.getContext().req;
      }
  }
  </code></pre>
  <p>roles.guard.ts</p>
  <pre><code>import { CanActivate, ExecutionContext, Injectable } from &quot;@nestjs/common&quot;;
  import { Reflector } from &quot;@nestjs/core&quot;;
  import { GqlExecutionContext } from &quot;@nestjs/graphql&quot;;
  
  @Injectable()
  export class RolesGuard implements CanActivate {
      constructor(private reflector: Reflector) {}
  
      canActivate(context: ExecutionContext) {
          const roles = this.reflector.get&lt;string[]&gt;(&quot;roles&quot;, context.getHandler());
          const ctx = GqlExecutionContext.create(context);
          console.log(&quot;roles: &quot;, roles);
          console.log(&quot;context: &quot;, context.switchToHttp().getRequest());
          console.log(&quot;gqlContext: &quot;, ctx.getContext().req);
  
          return true;
      }
  }
  </code></pre>
  <p>jwt.strategy.ts</p>
  <pre><code>import { Injectable } from &quot;@nestjs/common&quot;;
  import { PassportStrategy } from &quot;@nestjs/passport&quot;;
  import { ExtractJwt, Strategy } from &quot;passport-jwt&quot;;
  import { jwtConstants } from &quot;../constants&quot;;
  
  @Injectable()
  export class JwtStrategy extends PassportStrategy(Strategy) {
      constructor() {
          super({
              jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
              ignoreExpiration: false,
              secretOrKey: jwtConstants.secret,
          });
      }
  
      validate(payload: any) {
          console.log(&quot;payload: &quot;, payload);
  
          return payload;
      }
  }
  </code></pre>
  <p>resolver</p>
  <pre><code>@UseGuards(GqlAuthGuard)
  @Roles(&quot;ADMIN&quot;)
  @UseGuards(RolesGuard)
  @Query((returns) =&gt; [Specialty], { nullable: &quot;itemsAndList&quot;, name: &quot;specialties&quot; })
  async getSpecialties(@Args() params: FindManySpecialtyArgs, @Info() info: GraphQLResolveInfo) {
      const select = new PrismaSelect(info).value;
      params = { ...params, ...select };
      return this.prismaService.specialty.findMany(params);
  }
  </code></pre>
  <p>Has anyone successfully implemented this before ?</p>