Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: Class constructor MixinStrategy cannot be invoked without 'new'

I was following along with the jwt example like found here https://docs.nestjs.com/techniques/authentication. I copied and pasted the example. After npm installing the necessary bits and bops I got this error which does not occur in the sample which I just copied. Of which I have no idea what it means! Anyone any ideas?

TypeError: Class constructor MixinStrategy cannot be invoked without 'new'

   8 | export class JwtStrategy extends PassportStrategy(Strategy) {
   9 |   constructor(private readonly authService: AuthService) {
> 10 |     super({
  11 |       jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
  12 |       secretOrKey: 'secretKey',
  13 |     });

  at new JwtStrategy (data/auth/strategies/jwt.strategy.ts:10:5)
  at resolveConstructorParams (../node_modules/@nestjs/core/injector/injector.js:64:84)
  at Injector.resolveConstructorParams (../node_modules/@nestjs/core/injector/injector.js:86:30)
like image 965
wendellmva Avatar asked Jun 02 '18 08:06

wendellmva


1 Answers

The project lacks @types/passport-jwt typings, so they should be additionally installed:

npm i -D @types/passport-jwt

This results in

src\auth\jwt.strategy.ts (10,6): Call target does not contain any signatures. (2346)

error, because @nestjs/passport wasn't properly typed; PassportStrategy return type is any.

In order to fix this,

@Injectable()
export class JwtStrategy extends PassportStrategy(Strategy) {
...

should be changed to:

import { ExtractJwt, Strategy } from 'passport-jwt';
import { AbstractStrategy, PassportStrategy } from '@nestjs/passport';
...
const PassportJwtStrategy: new(...args) => AbstractStrategy & Strategy = PassportStrategy(Strategy);

@Injectable()
export class JwtStrategy extends PassportJwtStrategy {
...
like image 152
Estus Flask Avatar answered Oct 09 '22 23:10

Estus Flask