【问题标题】:TypeError: cb is not a function at secretProvider in node.js back-end api using express-jwt类型错误:cb 不是使用 express-jwt 的 node.js 后端 api 中 secretProvider 的函数
【发布时间】:2023-01-04 21:09:10
【问题描述】:
TypeError: cb is not a function
    at secretProvider 

我得到这里的错误是我使用 express 和 jwt 连接到 Auth0 的 index.js 文件

const express = require('express')
const bodyParser = require('body-parser')
const { expressjwt: jwt } = require("express-jwt");
const jwksRsa = require('jwks-rsa');
const envVariables = require('./env-variables.json');
const db = require('./queries')
const port = 3000
const app = express()
app.use(bodyParser.json())
app.use(
  bodyParser.urlencoded({
    extended: true,
  })
)

app.use(jwt({
  // Dynamically provide a signing key based on the kid in the header and the singing keys provided by the JWKS endpoint.
  secret: jwksRsa.expressJwtSecret({
    cache: true,
    rateLimit: true,
    jwksRequestsPerMinute: 5,
    jwksUri: `https://${envVariables.auth0Domain}/.well-known/jwks.json`,
  }),

  // Validate the audience and the issuer.
  audience: envVariables.apiIdentifier,
  issuer: `https://${envVariables.auth0Domain}/`,
  algorithms: ['RS256']
}));
app.get('/private', (req, res) => res.send('Only authenticated users can read this message.'));
app.get('/users', db.getUsers)
app.get('/users/:id', db.getUserById)
app.post('/users', db.createUser)
app.put('/users/:id', db.updateUser)
app.delete('/users/:id', db.deleteUser)

app.listen(port, () => {
  console.log(`App running on port ${port}.`)
})

我不知道该怎么做我是后端新手,问题可能是在我更新依赖项时开始的,现在与我使用的教程不同

【问题讨论】:

    标签: node.js express jwt


    【解决方案1】:

    自从您在这里提出问题后,我不知道您是否找到了解决方法。我只是面临与您完全相同的问题。

    它似乎来自 express 4.x 和 jwksRsa.expressJwtSecret 之间的不兼容。

    我没有成功修复它。手动使用另一个依赖项来解决问题。

    节点-oauth2-jwt-承载你可以在这里找到 git 页面:https://github.com/auth0/node-oauth2-jwt-bearer/tree/main/packages/express-oauth2-jwt-bearer

    基本上以你的例子它会变成:

    const express = require('express')
    const bodyParser = require('body-parser')
    const { auth } = require("express-oauth2-jwt-bearer");
    const envVariables = require('./env-variables.json');
    const db = require('./queries')
    const port = 3000
    const app = express()
    app.use(bodyParser.json())
    app.use(
      bodyParser.urlencoded({
        extended: true,
      })
    )
    
    app.use(auth ({
      audience: envVariables.apiIdentifier,
      issuerBaseURL: `https://${envVariables.auth0Domain}/`,
      tokenSigningAlg: 'RS256'
    }));
    app.get('/private', (req, res) => res.send('Only authenticated users can read this message.'));
    app.get('/users', db.getUsers)
    app.get('/users/:id', db.getUserById)
    app.post('/users', db.createUser)
    app.put('/users/:id', db.updateUser)
    app.delete('/users/:id', db.deleteUser)
    
    app.listen(port, () => {
      console.log(`App running on port ${port}.`)
    })
    

    如果您仍然遇到问题,您可以通过该教程找到一些帮助:https://auth0.com/docs/quickstart/backend/nodejs/interactive#configure-the-middleware

    希望能帮助到你

    【讨论】:

      【解决方案2】:

      只需更新 jwks-rsa 库

      npm update jwks-rsa
      

      确保您的 package.json 中有该库的插入符号,如下所示:

      "jwks-rsa": "^2.1.5",
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-07-29
        • 2023-03-23
        • 1970-01-01
        • 2018-09-29
        • 2019-11-09
        • 2015-11-05
        • 2019-02-05
        • 2017-10-14
        相关资源
        最近更新 更多