JwtStrategy requires a secret or key

See original GitHub issue

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?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8

github_iconTop GitHub Comments

44reactions
aabanaagcommented, May 11, 2020

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

28reactions
bunny234commented, Dec 26, 2020

Check “local.strategy.ts” if you have one and replace import { Strategy } from ‘passport-jwt’; with import { Strategy } from ‘passport-local’;

Read more comments on GitHub >

github_iconTop Results From Across the Web

NodeJS TypeError('JwtStrategy requires a secret or key')
Met the same issue. It was just because the first options argument passed to JwtStrategy must have a valid secretOrKey .
Read more >
[Solved]-Why do I get error JwtStrategy requires a secret or key?
Coding example for the question Why do I get error JwtStrategy requires a secret or key?-mongodb.
Read more >
Passport authentication strategy using JSON Web Tokens
function passport-jwt.Strategy.prototype.authenticate (req, options) ... { throw new TypeError('JwtStrategy requires a secret or key'); } this.
Read more >
18 Nest JS JWT Secret through config and env file - YouTube
The secret string is used while signing the payload and also when decoding it inside our JWT strategy. So, it's important that the...
Read more >
ERROR [ExceptionHandler] JwtStrategy requires a secret or key
local.strategy.ts에서 passport-local로 해줘야 되는데 passport-jwt로 선언해서 안됐다. 심지어 구글링에서 계속 봤는데 영어 해석을 반대로 해서 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found