从 Auth0.js 迁移到 Auth0-spa.js - Jwt 格式错误

Migrating from Auth0.js to Auth0-spa.js - Jwt malformed

我正在尝试将我的应用程序从 auth0 迁移到 auth0-spa.js。我已经能够让它几乎正常工作,而且这个新库的代码肯定更简单,所以我想继续使用它,但我还需要一个有效的后端 jwt 令牌。

我在我的节点服务器 (express-jwt) 上使用了以下中间件

export const jwtCheckMiddleware = jwt({
    secret: '....',
    getToken: function (req) {
       if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
           return req.headers.authorization.split(' ')[1];
       }
    },
    issuer: `https://${environment.auth.auth0Domain}/`,
    algorithm: 'RS256'
});

以前我会从 auth0 传递 idToken 并且它有效。现在我通过 await auth0.getTokenSilently() 获得了一个令牌,但是将它传递给中间件给我 "jwt malformed".

如何从 auth0-spa.js 获取有效的 JWT 令牌?另外,我如何确保传递给中间件的令牌永不过期?

好的,通过向 Auth0 注册自定义 API 然后将 API 标识符作为观众传递,我能够获得 auth-spa.js 给我一个 jwt 令牌。

更多信息在这里:https://auth0.com/docs/getting-started/set-up-api

const auth0 = createAuth0Client({
  audience: environment.auth.audience,  <-----API identifier of custom API in Auth0
  client_id: environment.auth.clientId,
  domain: environment.auth.clientDomain
});

添加受众后,getTokenSilently() 给了我一个 JWT 令牌,我将其传递给节点。然后我不得不在我的 jwt 中间件中添加观众:

export const jwtCheckMiddleware = jwt({
    secret: jwks.expressJwtSecret({
        cache: true,
        rateLimit: true,
        jwksRequestsPerMinute: 5,
        jwksUri: `https://${environment.auth.auth0Domain}/.well-known/jwks.json`
    }),
    audience: environment.auth.auth0ApiAudience,  <!--- API identifier
    issuer: `https://${environment.auth.auth0Domain}/`,
    algorithms: ['RS256']
});