mikenicholson / passport-jwt

Passport authentication using JSON Web Tokens
MIT License
1.97k stars 214 forks source link

JwtStrategy requires a secret or key #206

Closed aabanaag closed 4 years ago

aabanaag commented 4 years ago

I get this error JwtStrategy requires a secret or key I followed the steps from NestJS on how to integrate passport for authentication.

constructor(private readonly authService: AuthService) {
    super({
      jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
      secretOrKey: process.env.JWT_SECRET
    })

    console.log(process.env.JWT_SECRET)
    console.log(this)
  }

the console looks like this

JwtStrategy {
  name: 'jwt',
  _secretOrKeyProvider: [Function],
  _verify: [Function],
  _jwtFromRequest: [Function],
  _passReqToCallback: undefined,
  _verifOpts: {
    audience: undefined,
    issuer: undefined,
    algorithms: undefined,
    ignoreExpiration: false
  },
  authService: AuthService {
    repository: Repository {
      manager: [EntityManager],
      metadata: [EntityMetadata],
      queryRunner: undefined
    },
    userService: UserService { repository: [Repository] }
  }
}

The full error:

[Nest] 3713   - 05/11/2020, 2:42:42 PM   [ExceptionHandler] JwtStrategy requires a secret or key +14ms
TypeError: JwtStrategy requires a secret or key
    at new JwtStrategy (/Users/jongbanaag/Documents/code/sideline/hyperspotters-api/node_modules/passport-jwt/lib/strategy.js:45:15)
    at new MixinStrategy (/Users/jongbanaag/Documents/code/sideline/hyperspotters-api/node_modules/@nestjs/passport/dist/passport/passport.strategy.js:31:13)
    at new LocalStrategy (/Users/jongbanaag/Documents/code/sideline/hyperspotters-api/dist/src/auth/local.strategy.js:18:9)
    at Injector.instantiateClass (/Users/jongbanaag/Documents/code/sideline/hyperspotters-api/node_modules/@nestjs/core/injector/injector.js:289:19)
    at callback (/Users/jongbanaag/Documents/code/sideline/hyperspotters-api/node_modules/@nestjs/core/injector/injector.js:76:41)
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
    at async Injector.resolveConstructorParams (/Users/jongbanaag/Documents/code/sideline/hyperspotters-api/node_modules/@nestjs/core/injector/injector.js:117:24)
    at async Injector.loadInstance (/Users/jongbanaag/Documents/code/sideline/hyperspotters-api/node_modules/@nestjs/core/injector/injector.js:80:9)
    at async Injector.loadProvider (/Users/jongbanaag/Documents/code/sideline/hyperspotters-api/node_modules/@nestjs/core/injector/injector.js:37:9)
    at async Promise.all (index 4)
✨  Done in 11.06s.

Any ideas?

aabanaag commented 4 years ago

Figured out the issue, on my local.strategy file instead of importing Strategy from passport-local it imported from passport-jwt which is causing the issue

Everett93 commented 4 years ago

What did you do to fix this? I am having same issue too and I can't seem to pin point what needs to be fixed

bunny234 commented 3 years ago

Check "local.strategy.ts" if you have one and replace import { Strategy } from 'passport-jwt'; with import { Strategy } from 'passport-local';

creatipos commented 3 years ago

Resolví el problema, en mi archivo local.strategy en lugar de importar la estrategia de passport-local que importó de passport-jwt que está causando el problema

this worked for me

juankrlos1 commented 3 years ago

Figured out the issue, on my local.strategy file instead of importing Strategy from passport-local it imported from passport-jwt which is causing the issue

this worked for me, excellent!

manikbajaj commented 2 years ago

It worked for me as well. But I would like to know why is it this way? If anyone can explain would really help, since the entire reason for importing from passport-jwt is that we want to implement that particular strategy

gbubemi22 commented 2 years ago

thank you for that passport-local

MahmooudTarek commented 2 years ago

gooood

idarciooliveira commented 1 year ago

thanks

Figured out the issue, on my local.strategy file instead of importing Strategy from passport-local it imported from passport-jwt which is causing the issue

thanks you help me alot

pdducthe commented 1 year ago

Figured out the issue, on my local.strategy file instead of importing Strategy from passport-local it imported from passport-jwt which is causing the issue

Thank you !!! I could use your idea to solve my similar problem

htiuk3 commented 10 months ago

Figured out the issue, on my local.strategy file instead of importing Strategy from passport-local it imported from passport-jwt which is causing the issue

thanks so much, this helped me

ibrex29 commented 6 months ago

thanks boss

mohit-761 commented 1 month ago

thank you so much this worked for me ..

hillonyechekwa commented 3 weeks ago

Hi, I have this same issue but I tried this suggestion and it didn't work for me.

here's what my jwt strategy looks like

export class JwtStrategy extends PassportStrategy(Strategy) {
    constructor(
        @Inject(jwtConfig.KEY)
        private jwtConfiguration: ConfigType<typeof jwtConfig>,
        private authService: AuthService
    ) {
        super({
            jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
            secretOrKey: jwtConfiguration.secret,
            ignoreExpiration: false
        });
    }

    async validate(payload: PayloadType) {
        const userId = payload.userId
        return this.authService.validateJwtUser(userId)
    }
}

and here's what my error is:

TypeError: JwtStrategy requires a secret or key
    at new JwtStrategy (C:\Users\Hill\Documents\logicX\numen\iluvia\fs_one\lrd_bck\node_modules\passport-jwt\lib\strategy.js:45:15)
    at new MixinStrategy (C:\Users\Hill\Documents\logicX\numen\iluvia\fs_one\lrd_bck\node_modules\@nestjs\passport\dist\passport\passport.strategy.js:32:13)    at GoogleStrategy (C:\Users\Hill\Documents\logicX\numen\iluvia\fs_one\lrd_bck\src\auth\google.strategy.ts:18:9)
    at Injector.instantiateClass (C:\Users\Hill\Documents\logicX\numen\iluvia\fs_one\lrd_bck\node_modules\@nestjs\core\injector\injector.js:365:19)
    at callback (C:\Users\Hill\Documents\logicX\numen\iluvia\fs_one\lrd_bck\node_modules\@nestjs\core\injector\injector.js:65:45)
    at async Injector.resolveConstructorParams (C:\Users\Hill\Documents\logicX\numen\iluvia\fs_one\lrd_bck\node_modules\@nestjs\core\injector\injector.js:144:24)
    at async Injector.loadInstance (C:\Users\Hill\Documents\logicX\numen\iluvia\fs_one\lrd_bck\node_modules\@nestjs\core\injector\injector.js:70:13)        
    at async Injector.loadProvider (C:\Users\Hill\Documents\logicX\numen\iluvia\fs_one\lrd_bck\node_modules\@nestjs\core\injector\injector.js:97:9)
    at async C:\Users\Hill\Documents\logicX\numen\iluvia\fs_one\lrd_bck\node_modules\@nestjs\core\injector\instance-loader.js:56:13
    at async Promise.all (index 7)