首页 > 解决方案 > JWT 令牌使用 Auth0 错过 Nuxt Auth 模块中的声明

问题描述

成功登录后,在我的 Nuxt 应用程序中,我收到了 Auth0 的声明。Devtools 控制台显示它:

$vm0.$auth.$state.user['https://hasura.io/jwt/claims']
{x-hasura-default-role: "user", x-hasura-allowed-roles: Array(1), x-hasura-user-id: "auth0|5e989a*******"}

但是我的令牌没有这个要求,所以我的 hasura 请求失败了。我在控制台中声明后立即检查令牌:

$vm0.$auth.getToken('auth0')
"Bearer eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6Ik1ESTNNa0l5...

我使用https://jwt.io/对其进行解码并且没有声明:

{
"iss": "https://*******.eu.auth0.com/",
  "sub": "auth0|5e989a*******",
  "aud": [
    "https://*******.eu.auth0.com/api/v2/",
    "https://*******.eu.auth0.com/userinfo"
  ],
  "iat": 1587059738,
  "exp": 1587066938,
  "azp": "MxJB0Y9dxvdGZnTAb2a4Y0YOHArvbkWt",
  "scope": "openid profile email"
}

这是我在 Auth0 仪表板中的声明脚本:

function (user, context, callback) {
const namespace = "https://hasura.io/jwt/claims";
  context.idToken[namespace] = 
    { 
      'x-hasura-default-role': 'user',
      // do some custom logic to decide allowed roles
      'x-hasura-allowed-roles': ['user'],
      'x-hasura-user-id': user.user_id
    };
  callback(null, user, context);
}

标签: jwtnuxt.jsauth0hasura

解决方案


我通过更改 Auth0 仪表板中的声明脚本解决了这个问题。它应该context.accessToken[namespace] = {...}代替,context.idToken[namespace] = {...}现在我的令牌中有该声明数据。


推荐阅读