mikenicholson / passport-jwt

Passport authentication using JSON Web Tokens
MIT License
1.96k stars 213 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 2 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 1 year 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 7 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 2 months ago

thanks boss