nestjs / javascript-starter

Nest framework JavaScript (ES6, ES7, ES8) + Babel starter 🍰
https://nestjs.com/
117 stars 26 forks source link

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

Closed erikash closed 3 years ago

erikash commented 5 years ago

Bug Report

I've started a new Node.js project and followed the passport authentication tutorial until: https://docs.nestjs.com/techniques/authentication#jwt-functionality

Current behavior

Expected behavior

Applications should start.

Environment

My jsconfig.json file (I'v changed to esnext as suggested by https://github.com/nestjs/passport/issues/27:

{
  "compilerOptions": {
      "target": "esnext",
      "experimentalDecorators": true
  },
  "exclude": [
      "node_modules"
  ]
}

Nest version: 6.7.2


For Tooling issues:
- Node version: v12.6.0  
- Platform:  Mac 

Others:

I've also edited local.strategy.js, i've switched the super(); and this.authService = authService; lines as I was getting an error: TypeError: Cannot set property 'authService' of undefined

local.strategy.js

import { Strategy } from 'passport-local';
import { PassportStrategy } from '@nestjs/passport';
import { Injectable, UnauthorizedException, Dependencies } from '@nestjs/common';
import { AuthService } from './auth.service';

@Injectable()
@Dependencies(AuthService)
export class LocalStrategy extends PassportStrategy(Strategy) {
  constructor(authService) {
    super();
    this.authService = authService;
  }

  async validate(username, password) {
    const user = await this.authService.validateUser(username, password);
    if (!user) {
      throw new UnauthorizedException();
    }
    return user;
  }
}
erikash commented 5 years ago

I've started a project using the typescript-starter and didn't encounter this issue.

sebas-deedee commented 5 years ago

Same issue here with babel... any update?

kamilmysliwiec commented 5 years ago

Please, provide a minimal repository which reproduces your issue.

sebas-deedee commented 5 years ago

@kamilmysliwiec please take a look on this repo: https://github.com/sebas-deedee/project-mercury-api

This repo only have the code example for authentication and the mentioned problem.

sebas-deedee commented 5 years ago

Any update @kamilmysliwiec ?

chakaponi commented 4 years ago

Nestjs no longer supports JS. TS only Please correct the support information on the official website

chakaponi commented 4 years ago

@erikash @kamilmysliwiec @sebas-deedee, I found temporary solution: In .babelrc (or babel.config.js (or .json)) need to change "presets": ["@babel/preset-env"] to "presets": [["@babel/preset-env", { "targets": { "node": "current" } }]]

The point is that JS, unlike TS, does not perceive "compilerOptions", which leads to an attempt of interaction between ES5 and ES6 code

saheljalal commented 4 years ago

Hi, I'm running into the exact same error but my project was set up differently so not sure if relevant but I'm hoping there is a workaround.

I set up a NativeScript shared project with Angular and then used the NextJS schematic to convert the project into an Angular Universal project. After adding the passport dependency and a local strategy described in the docs I ran into this error.

My tsconfig.json shows the target is set to es5 and changing it to es6 or others seems to cause other compilation issues. Not even sure if NativeScript/Angular is compatible with NextJS though. Any tips would be much appreciated 🙏