NestJS -- JWT Dependency is not being loaded

Viewed 557

I do not need complete passport, I just need to sign a JWT token.

I tried working with this repository I tried all possible combinations, but I just cant integrate it in the project. I had followed the course of several different errors. I fix one, and the another pops up. So, I am including minimum part, and the current error that is thrown.

AuthModule:

import { Module } from '@nestjs/common';
import { JwtModule } from '@nestjs/jwt';
import { AuthService } from './auth.service';
import { JwtService } from '@nestjs/jwt';

@Module({
  imports: [ JwtModule.register({ secret: 'hard!to-guess_secret' })],
  providers: [AuthService],
  exports: [AuthService, JwtService]
})
export class AuthModule {}

AuthService:

    import { Injectable } from '@nestjs/common';
import { UsersService } from '../users/users.service';
import { JwtService } from '@nestjs/jwt';

@Injectable()
export class AuthService {
  constructor(
      private readonly jwtService: JwtService,
  ) {}

  async signPayload (user: any) {
    const payload = { username: 'HARDCORE', color:'RED' };
    return {
      whatever: this.jwtService.sign(payload),
    };
  }
}

AppModule:

@Module({
  imports: [
    ConfigModule.forRoot(),
    JwtModule.registerAsync({
      imports: [ConfigModule],
      useFactory: async (configService: ConfigService) => ({
        secret: 'wefwefwef',
      }),
      inject: [ConfigService],
    }),
    TypeOrmModule.forRoot({
      type: 'mysql',
      ...
    }),
    UsersModule,
    SubscriptionsModule,
    ProductsModule
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {
}

SomeController:

export class UsersController {
  constructor(private readonly usersService: UsersService,
              private readonly authService: AuthService
  ) {}
...
 @ApiResponse({ status: 401, description: 'Unauthorized.' })
  @ApiResponse({ status: 404, description: 'User Not Found.' })
  @Get('users')
  async findOne(@Query() userGetDto: UserGetDto): Promise<User> {
    const user = await this.usersService.findByUsername(userGetDto.userName);
    if (!user) throw new NotFoundException('User Not Found')
     let signedUser = this.authService.signPayload(user);
    return user;

And this is the error with this setup that I get:

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

I spend lot of time on this one, but I just cant make it work.

1 Answers

Based on your error, JwtService is in an imports array somewhere. Providers (classes marked with @Injectable()) never go in the imports array, but rather should be added to the exports array and that module where the exports was added should be put in the consuming module's imports array.

Also, if you are working with a Dynamic Module (any module that uses register or forRoot or an async variant of the two) you should always export the module instead of its services, as the module most likely has important configurations necessary for the service to work.

Related