Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

NestJS JWT Strategy: 'super' must be called before accessing 'this' in the constructor of a derived class

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

'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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading