gpt4 book ai didi

node.js - 为什么会出现错误 JwtStrategy requires a secret or key?

转载 作者:行者123 更新时间:2023-12-04 12:38:18 25 4
gpt4 key购买 nike

我收到此错误“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)
    });
    })
    )
    }

    按键.js
    module.exports = {
    secretOrKey: 'secret'
    };

    最佳答案

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

    opts.secretOrKey = keys.secretOrKey

    你错误地写了 opts.secretKey。
    希望这可以帮助!

    关于node.js - 为什么会出现错误 JwtStrategy requires a secret or key?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58841660/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com