7

I have a question about setting environmental variables.

In official document, it says using ConfigModule in this case, but my case is a exception case.

Because I would like to use it in super() in constructor.

My code is the below.

Is there any solution in this case?

If you need more information, please let me know.

Thank you all your support!!

// jwt.strategy.ts

import { UnauthorizedException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { PassportStrategy } from '@nestjs/passport';
import { InjectRepository } from '@nestjs/typeorm';
import { Strategy, ExtractJwt } from 'passport-jwt';
import { JwtPayload } from './jwt-payload.interface';
import { UserRepository } from './user.repository';
export class JwtStrategy extends PassportStrategy(Strategy) {
    constructor(
        @InjectRepository(UserRepository)
        private userRepository: UserRepository,
        private configService: ConfigService,
    ) {
        super({
            jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
            secretOrKey: configService.get('JWT_TOKEN'),
        });
    }

    async validate(payload: JwtPayload) {
        const { username } = payload;
        const user = await this.userRepository.findOne({ username });

        if (!user) {
            throw new UnauthorizedException();
        }
        return user;
    }
}
Krzysztof Madej
  • 32,704
  • 10
  • 78
  • 107
shun
  • 71
  • 1
  • 2
  • 2
    check this answer may it works for you : [configService o pass secret key](https://stackoverflow.com/questions/50977202/nestjs-jwtstrategy-use-configservice-to-pass-secret-key) – Youba Nov 19 '20 at 10:28

3 Answers3

6

I would also like to point out that since you are not using the configService anywhere but in the super() call the usage of private keyword in front of it is redundant

You can try using this.configService.get('JWT_TOKEN') but it will simply yell at you that you haven't called super

Removing the private keyword will simply avoid making the configService as a class variable and simply treat it as some option passed into it

aekant
  • 199
  • 1
  • 5
  • 4
    This should be the correct answer. Because you can't access `this` inside `super` call, remove the `private` keyword and get config by `configService.get('JWT_TOKEN')`. – ryanhex53 Jan 12 '22 at 15:18
  • 1
    Agree with the previous comment, this should be the accepted answer. Thanks @aekant, it helped – Alex Mantaut May 25 '23 at 07:36
1

For your Strategy you're missing the @Injectable() which tells Nest that it needs to inject the dependencies defined in the constructor.

Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147
1

You need to import the configModule into your module class for the configService to work. Also, add @Injectable() right above the class name to indicate it is a provider.

This is how you import the module.

//auth.module.ts    
    
import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { JwtStrategy } from './jwt.strategy';

@Module({
  imports: [ConfigModule],
  provider:[JwtStrategy]
})
export class AuthModule {}

NestJs resolves the dependencies between them.

see: https://docs.nestjs.com/techniques/configuration#using-the-configservice

Xwebyna
  • 54
  • 5