【问题标题】:Why do I get error JwtStrategy requires a secret or key?为什么我会收到错误 JwtStrategy requires a secret or key?
【发布时间】:2020-03-09 12:27:54
【问题描述】:

我收到此错误“TypeError: JwtStrategy requires a secret or key”,我不知道如何修复它。我能做些什么来解决它?

(node:46218) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 exit listeners added to [Bus]. Use emitter.setMaxListeners() to increase limit
/mnt/data/Workspace/Development/comichaven/api/node_modules/passport-jwt/lib/strategy.js:45
        throw new TypeError('JwtStrategy requires a secret or key');
        ^

TypeError: JwtStrategy requires a secret or key
    at new JwtStrategy (/mnt/data/Workspace/Development/comichaven/api/node_modules/passport-jwt/lib/strategy.js:45:15)
    at module.exports (/mnt/data/Workspace/Development/comichaven/api/config/passport.js:14:9)
    at Object.<anonymous> (/mnt/data/Workspace/Development/comichaven/api/server.js:40:29)
    at Module._compile (internal/modules/cjs/loader.js:956:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
    at internal/main/run_main_module.js:17:11
[nodemon] app crashed - waiting for file changes before starting...

我尝试过的

  • 重命名变量 opts
  • 已检查,因此变量命名相同
  • 尝试使用 dotenv
  • 用字符串替换keys.secretOrKey

passport.js

const JwtStrategy = require('passport-jwt').Strategy;
const ExtractJwt = require('passport-jwt').ExtractJwt;
const mongoose = require('mongoose');
const User = mongoose.model("users");
const keys = require("./keys")

const opts = {};

opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretKey = keys.secretOrKey;

module.exports = passport => {
    passport.use(
        new JwtStrategy(opts, (jwt_payload, done) => {
            User.findById(jwt_payload.id)
            .then(user => {
                if (user) {
                    return done(null, user)
                }
                return done(null, false)
            })
            .catch(err => {
                console.log(err)
            });
        })
    )
}

keys.js

module.exports = {
    secretOrKey: 'secret'
};

【问题讨论】:

  • 你能确认keys.secretOrKey;上面实际上持有预期的价值。

标签: node.js mongodb express passport.js


【解决方案1】:

您必须在选项中为 secretkey 使用正确的密钥名称。你应该这样写:-

opts.secretOrKey = keys.secretOrKey

你写错了 opts.secretKey。 希望这会有所帮助!

【讨论】:

    【解决方案2】:

    1.你也可以安装 mongoDB:

    https://docs.mongodb.com/manual/tutorial/install-mongodb-on-ubuntu/

    2。里面keys文件:

    module.exports = {
      mongoURI: process.env.MONGO_URI,
      jwt: process.env.JWT
    }
    

    3.安装和配置服务器时,使用终端/命令行 附加键值。就我而言,它是:

    export NODE_ENV=production
    export JWT=jwt-prod
    export MONGO_URI=mongodb://localhost/db
    

    【讨论】:

      猜你喜欢
      • 2019-01-14
      • 2021-07-27
      • 1970-01-01
      • 2020-11-05
      • 2011-04-14
      • 2020-11-05
      • 2018-06-22
      • 2020-01-12
      • 2016-01-01
      相关资源
      最近更新 更多