I’m learning to create an authentication system with NestJS using JWT. My system has a CRUD for managing users but it can only be used by authenticated people, so I put this guard:
@Controller('users')
@UseGuards(AuthGuard('jwt'))
export class UsersController {
\\ implementation
}
I’m trying to create the JWT strategy:
@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
constructor(private readonly config: ConfigService) {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
ignoreExpiration: false,
secretOrKey: this.config.get<string>('JWT_SECRET_PASS'),
});
}
}
But facing the error:
'super' must be called before accessing 'this' in the constructor of a derived class.
I need to access the ConfigService so I can define my secret which is in an env. How can I do that?
>Solution :
config is passed as a parameter to the constructor, so you can remove the this in this.config and still access config just fine