Anonymous Creator
Anonymous Creator

Reputation: 3789

NestJs jwt auth global guards TypeError: Cannot read property 'secretOrKeyProvider' of undefined

I am using global filters as below.

const server = await NestFactory.create(ApplicationModule);
server.useGlobalGuards(new (AuthGuard('jwt')));

My applicationmodule looks like below.

import { Module } from '@nestjs/common';
import { JwtModule, JwtModuleOptions } from '@nestjs/jwt';
import { PassportModule } from '@nestjs/passport';
import { Strategy } from 'passport-jwt';
import { AppController } from './application.controller';
import { ConfigModule, ConfigType } from '@nestjs/config';
import jwtConfig from './config/jwt.config';

@Module({
  imports: [
    ConfigModule.forFeature(jwtConfig),
    PassportModule.register({ defaultStrategy: 'jwt' }),
    JwtModule.registerAsync({
      imports: [ConfigModule.forFeature(jwtConfig)],
      useFactory: (config: ConfigType<typeof jwtConfig>) => {
        return {
          secret: config.secretKey,
          signOptions: { expiresIn: config.expiresIn },
        } as JwtModuleOptions;
      },
      inject: [jwtConfig.KEY],
    }),
  ],
  controllers: [
    AppController
  ],
  providers: [Strategy],
})
export class ApplicationModule {}

and I am getting below error.

TypeError: Cannot read property 'secretOrKeyProvider' of undefined

What am I missing? I am unable to find any example where Global auth guards are used.

Upvotes: 0

Views: 1923

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70161

A JwtStrategy (or some other passport strategy class) needs to be created. This logic is different per server/application, so it's impossible to say what generally. The Nest docs do a good job of showing an example.

Upvotes: 1

Related Questions