title: Inject service into guard in Nest.JS
body: <p>I have KeysModule, which can be used to add or remove API keys. I need these keys to protect some routes from unauthorized access. 
  To protect these routes I have created ApiGuard:</p>
  
  <pre><code>import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';
  
  @Injectable()
  export class ApiGuard implements CanActivate {
  
  async canActivate(
      context: ExecutionContext,
    ): Promise&lt;boolean&gt; {
      const request = context.switchToHttp().getRequest();
      return request.headers.api_key;
    }
  }
  </code></pre>
  
  <p>And then I use it in route:</p>
  
  <pre><code> @Get('/protected')
   @UseGuards(ApiGuard)
   async protected(@Headers() headers: Api) {
     const key = await this.ks.findKey({ key: headers.api_key });
     if (!key || !key.active) return 'Invalid Key';
     return 'Your API key works';
   }
  </code></pre>
  
  <p>Where ks is KeyService used to check if key is correct or not.
  This solution works, but is stupid. I have to copy and paste some lines of code everywhere I want to use this guard (I mean lines in route).</p>
  
  <p>I have tried to to move all logic to ApiGuard, but there I have got error, that KeyService cannot be injected to ApiGuard class. To explain, I have KeyService in providers in KeysModule, but ApiGuard is globally used.</p>
  
  <p>Do you have any idea how to do it?</p>