nestjs / passport

Passport module for Nest framework (node.js) 🔑

Home Page:https://nestjs.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

passport-http-bearer doesn’t work with NestJS: TypeError: Class extends value undefined is not a constructor or null at PassportStrategy

Joyescat opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Current behavior

I’m trying to use passport-http-bearer in my nestjs project, but passport-http-bearer seems incompatible with NestJS.
When I start my application, I get the following error:

[11:36:00 AM] File change detected. Starting incremental compilation...

[11:36:01 AM] Found 0 errors. Watching for file changes.


/home/jpellat/passport-bux/node_modules/@nestjs/passport/dist/passport/passport.strategy.js:15
    class MixinStrategy extends Strategy {
                                ^
TypeError: Class extends value undefined is not a constructor or null
    at PassportStrategy (/home/jpellat/passport-bux/node_modules/@nestjs/passport/dist/passport/passport.strategy.js:15:33)
    at Object.<anonymous> (/home/jpellat/passport-bux/project-name/src/strategy.ts:4:47)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1101:10)
    at Module.load (internal/modules/cjs/loader.js:937:32)
    at Function.Module._load (internal/modules/cjs/loader.js:778:12)
    at Module.require (internal/modules/cjs/loader.js:961:19)
    at require (internal/modules/cjs/helpers.js:92:18)
    at Object.<anonymous> (/home/jpellat/passport-bux/project-name/src/app.module.ts:5:1)
    at Module._compile (internal/modules/cjs/loader.js:1072:14)

Minimum reproduction code

https://github.com/Joyescat/passport-bug

Steps to reproduce

  1. Create a nestjs project with the cli
$ npm i -g @nestjs/cli
$ nest new project-name
  1. Install dependencies npm i --save @nestjs/core @nestjs/common @nestjs/passport rxjs reflect-metadata passport-http-bearer

  2. Create the file strategy.ts in src/ directory

import { PassportStrategy } from '@nestjs/passport';
import { BearerStrategy } from 'passport-http-bearer';

export class Strategy extends PassportStrategy(BearerStrategy, 'okta') {
  constructor() {
    super();
  }

  validate(payload: string): any {
    console.log(payload);
    return payload;
  }
}
  1. Add the following lines in main.ts
import { NestFactory } from '@nestjs/core';
import * as passport from 'passport';

import { AppModule } from './app.module';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.use(passport.initialize());
  await app.listen(3000);
}
bootstrap();
  1. Add the new Stratgy as provider to the AppModule.ts
import { Module } from '@nestjs/common';

import { AppController } from './app.controller';
import { AppService } from './app.service';
import { Strategy } from './strategy';

@Module({
  imports: [],
  controllers: [AppController],
  providers: [AppService, Strategy],
})
export class AppModule {}
  1. start the application npm run start or npm run start:dev

Expected behavior

The application should start without any error.

Package version

8.0.1

Passport version

0.4.1

NestJS version

8.1.1

Node.js version

14.17.5

In which operating systems have you tested?

  • macOS
  • Windows
  • Linux

Other

  • Operating System: Windows 10 - WSL2 Ubuntu 20.04.3 LTS (Focal Fossa)
  • passport-http-bearer version: passport-http-bearer@1.0.1

Your minimum reproduction is an empty git repo. Please update it to a minimum, clonable, git repo that we can work with to see the same problem you're facing

I just pushed. My connection is slow. I didn’t expect my issue to be addressed so quickly.

Not a bug. Misconfiguration of the import. Use import * as BearerStrategy from 'passport-http-bearer'; and your error will be gone.