Inject service into guard in Nest.JS Inject service into guard in Nest.JS express express

Inject service into guard in Nest.JS


To inject service in guard.You can create a global module.

// ApiModuleimport {Module,Global} from '@nestjs/common';import {KeyService} from '../';@Global()@Module({    providers: [ KeyService ],    exports: [KeyService]})export class ApiModule {}

Then inject service into guard like this

// guardexport class ApiGuard implements CanActivate {constructor(@Inject('KeyService') private readonly KeyService) {}} async canActivate(context: ExecutionContext) {    // your code    throw new ForbiddenException();  }

Now the problem can be solved.But I have another problem.I want to inject something into service but got this error:

Nest can't resolve dependencies of the AuthGuard (?, +). Please make sure that the argument at index [0] is available in the current context.

And here is my solution:

To Inject other dependency in KeyService,like nestjs docs say.

global guards registered from outside of any module (with useGlobalGuards() as in the example above) cannot inject dependencies since this is done outside the context of any module.

This is their sampleļ¼š

// app.module.jsimport { Module } from '@nestjs/common';import { APP_GUARD } from '@nestjs/core';@Module({  providers: [    {      provide: APP_GUARD,      useClass: RolesGuard,    },  ],})export class ApplicationModule {}

It worked.Now I can use guard global without dependency error.


Maybe it's too late, but I ran the same issue and find a solution. Maybe there is a better one, but it's working properly for me:

Define KeysModule as a global module, you can check how to do it in nestjs docs: https://docs.nestjs.com/modules

After You can do this:

import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common';@Injectable()export class ApiGuard implements CanActivate {constructor(@Inject('KeyService')private readonly ks) {}const key = await this.ks.findKey();"YOUR_CODE_HERE..."}

Hope it's help to you or somebody who will stuck with this in the future.


You can inject a service in a guard like in any object annotated with Injectable.If your ApiGuard need KeyService you have two choices:

  • Add ApiGuard in a module which import KeysModule. Then import the created module to use ApiGuard globally
  • Add ApiGuard in KeysModule and export it.