title: How to get User Access Control in NestJs Guard for authorizations?
body: <p>I am new in @Nestjs. I am using graphql in @Nestjs. I have a question in this field. How can I access <code>UserSchema</code> or <code>UserModel</code> from <code>AuthGuard.ts</code>?</p>
  <p>Here in <code>AuthGuard.ts</code> I am checking <code>request context headers</code> is and verify <code>jwt</code> token-</p>
  <p>import { Injectable, CanActivate, ExecutionContext, HttpException, HttpStatus } from &quot;@nestjs/common&quot;;
  import { GqlExecutionContext } from &quot;@nestjs/graphql&quot;;
  import * as jwt from &quot;jsonwebtoken&quot;;</p>
  <pre><code>@Injectable()
  export class AuthGaurd implements CanActivate {
  
      async canActivate(context: ExecutionContext) {
          const ctx = GqlExecutionContext.create(context).getContext();
          if (!ctx.headers.authorization) {
              return false;
          }
          ctx.user = await this.validateToken(ctx.headers.authorization);
          console.log(ctx.user.info);
          return true;
      }
  
      validateToken(auth: string) {
          if (auth.split(' ')[0] !== 'Bearer') {
              throw new HttpException('Invalid token', HttpStatus.UNAUTHORIZED);
          }
          const token = auth.split(' ')[1];
          try {
              return jwt.verify(token, process.env.JWT_SECRET_KEY)
          } catch (err) {
              throw new HttpException(&quot;Invalid token&quot;, HttpStatus.UNAUTHORIZED)
          }
      }
  }
  </code></pre>
  <p>Here We find <code>Email as Info</code> from jwt token. Now I have to check this email is still registered, or this user role is user, creator and admin?</p>
  <p>I call this AuthGuard in my resolver-</p>
  <pre><code>import { Resolver, Mutation, Query, Args, Context } from &quot;@nestjs/graphql&quot;;
  import { UseGuards } from &quot;@nestjs/common&quot;;
  
  //Service
  import { QuestionService } from &quot;./question.service&quot;;
  //Guardss
  import { AuthGaurd } from &quot;src/helpers/auth.guard&quot;;
  //Entity
  import { CreateQuestionEntity } from &quot;./entities/create-question.entity&quot;;
  //Dto
  import { CreateQuestionInput } from &quot;./dto/create-question.input&quot;;
  
  @Resolver()
  export class QuestionResolver {
      //Constructor
      constructor(private readonly questionService: QuestionService) { }
  
      //Create Question Resolver
      @Mutation(() =&gt; CreateQuestionEntity, { name: &quot;addQuestion&quot; })
      @UseGuards(new AuthGaurd()) // Here I call AuthGuard
      createQuestion(
          @Args('createQuestionInput')
          createQuestionInput: CreateQuestionInput
      ) {
          return this.questionService.create(createQuestionInput)
      }
  }
  </code></pre>
  <p>Again, my question is- This Authguard.ts only check if user sent <code>request context header</code> and <code>verify jwt token</code>. After verifying <code>jwt</code> token I get email from this <code>jwt</code> token. After getting the email, I have to create 3 new Guards. One for checking this user still has in my database, second is if this user is creator, third is if this user is admin. And then I have to check when and which one I need. If I need a creator, then I have to call the creator guard. Or if I need admin then I have to call admin guard.</p>
  <p>I think I can clear my question? Please help me, How can I do that in my project?</p>
  <p><a href="https://i.stack.imgur.com/Ow17L.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Ow17L.png" alt="enter image description here" /></a></p>
  <p>This is my folder structure-</p>
  <p>in user folder I handle user creation, login, getting user-info
  in question folder I handle question creation, updating and getting</p>
  <p>in question folder I need to check user is Authorized or Creator or Admin.</p>
  <p>That's why I create one new folder named helper.</p>
  <p>in this helper I create AuthGuard.ts file
  here I need to create 2 more file for <code>CreatorGuard</code> and <code>AdminGuard</code>. and I have to check the roles.</p>
  <p>Here is my repo- <a href="https://github.com/siamahnaf198/nest-graphql" rel="nofollow noreferrer">https://github.com/siamahnaf198/nest-graphql</a></p>
  <p>Please give me the full instructions and examples.</p>