title: NestJS jwt-passport Authentication
body: <p>I want to implement a distributed authentication library to use it on several projects. The library should implement JWT authentication method. The code is as follows:</p>
  <p><strong>jwt.strategy.ts</strong></p>
  <pre><code>import {ExtractJwt, Strategy} from 'passport-jwt';
  import {PassportStrategy} from '@nestjs/passport';
  import {Injectable} from '@nestjs/common';
  import {JwtPayload, User} from './interfaces';
  import {ConfigService} from &quot;./config.service&quot;;
  
  @Injectable()
  export class JwtStrategy extends PassportStrategy(Strategy, 'jwt') {
    constructor(private readonly configService: ConfigService) {
      super({
        jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
        secretOrKey: configService.get('secretOrPrivateKey'),
      });
    }
  
    async validate(payload: JwtPayload): Promise&lt;User&gt; {
       return {
        uuid: payload.uuid,
        email: payload.email,
      }
    }
  }
  </code></pre>
  <p><strong>jwt.auth.module.ts:</strong></p>
  <pre><code>import {Module, DynamicModule} from '@nestjs/common';
  import {JwtModule} from '@nestjs/jwt';
  import {JwtStrategy} from './jwt.strategy';
  import {PassportModule} from '@nestjs/passport';
  import {ConfigService} from &quot;./config.service&quot;;
  import {JwtOptions} from &quot;./interfaces/jwt.options&quot;;
  
  @Module({
  })
  
  export class JwtAuthModule {
    static forRoot(jwtOptions): DynamicModule {
      return {
        module: JwtAuthModule,
        imports: [
          // JwtModule.register(jwtOptions),
          // PassportModule.register({defaultStrategy: 'jwt'}),
        ],
        providers: [
          JwtStrategy,
          {
            provide: ConfigService,
            useValue: new ConfigService(jwtOptions),
          }
        ],
        exports: [ConfigService, JwtStrategy]
      };
    }
  }
  </code></pre>
  <p>and I have imported this in my <code>app.module.ts</code>:</p>
  <pre><code>import { Module, NestModule, HttpModule } from '@nestjs/common';
  import { MongooseModule } from '@nestjs/mongoose';
  import { environment } from './environments';
  import { AuthModule } from './auth/auth.module';
  import { PermissionModule } from './permission/permission.module';
  import {JwtAuthModule} from '@pe/nest-kit';
  import {JwtModule} from '@nestjs/jwt';
  import {PassportModule} from '@nestjs/passport';
  
  @Module({
    imports: [
      JwtModule.register(environment.jwtOptions),
      PassportModule.register({defaultStrategy: 'jwt'}),
      JwtAuthModule.forRoot(environment.jwtOptions),
      HttpModule,
      AuthModule,
      PermissionModule,
      MongooseModule.forRoot(environment.mongodb),
    ],
  })
  export class ApplicationModule implements NestModule {
    configure() {
    }
  }
  </code></pre>
  <p>however, each time I try to open project url, I'm getting an error:</p>
  <blockquote>
  <p>[Nest] 27645   - 24.10.2018, 15:23:26   [ExceptionsHandler] Unknown
  authentication strategy &quot;jwt&quot; +4119ms Error: Unknown authentication
  strategy &quot;jwt&quot;
  at attempt (/home/user/workspace/permissions/node_modules/passport/lib/middleware/authenticate.js:187:37)
  at authenticate (/home/user/workspace/permissions/node_modules/passport/lib/middleware/authenticate.js:363:7)
  at Promise (/home/user/workspace/permissions/node_modules/@nestjs/passport/dist/auth.guard.js:83:3)
  at new Promise ()
  at /home/user/workspace/permissions/node_modules/@nestjs/passport/dist/auth.guard.js:75:83
  at MixinAuthGuard. (/home/user/workspace/permissions/node_modules/@nestjs/passport/dist/auth.guard.js:47:36)
  at Generator.next ()
  at /home/user/workspace/permissions/node_modules/@nestjs/passport/dist/auth.guard.js:19:71
  at new Promise ()
  at __awaiter (/home/user/workspace/permissions/node_modules/@nestjs/passport/dist/auth.guard.js:15:12)
  at MixinAuthGuard.canActivate (/home/user/workspace/permissions/node_modules/@nestjs/passport/dist/auth.guard.js:40:20)
  at GuardsConsumer.tryActivate (/home/user/workspace/permissions/node_modules/@nestjs/core/guards/guards-consumer.js:13:34)
  at canActivateFn (/home/user/workspace/permissions/node_modules/@nestjs/core/router/router-execution-context.js:97:59)
  at /home/user/workspace/permissions/node_modules/@nestjs/core/router/router-execution-context.js:47:37
  at /home/user/workspace/permissions/node_modules/@nestjs/core/router/router-proxy.js:8:23
  at Layer.handle [as handle_request] (/home/user/workspace/permissions/node_modules/express/lib/router/layer.js:95:5)</p>
  </blockquote>
  <p>what am I doing wrong?</p>