TypeError:cb不是使用express-jwt的node.js后端API中secretProvider的函数

olhwl3o2  于 2022-11-29  发布在  Node.js
关注(0)|答案(1)|浏览(138)
TypeError: cb is not a function
    at secretProvider

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

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}.`)
})

我不知道该怎么做我新的后端,问题可能已经开始时,我更新了依赖关系,这是现在不同的教程,我用

vsdwdz23

vsdwdz231#

我不知道你有没有找到解决的办法,因为你在这里提出了你的问题。
它似乎来自于express 4.x和jwksRsa之间的不兼容。
我没有成功地解决它。通过使用另一个依赖关系,我可以解决这个问题。

node-oauth2-jwt-bearer您可以在此处找到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
希望能有所帮助

相关问题