首页 > 解决方案 > Auth0 忽略范围选项

问题描述

我使用本指南https://auth0.com/docs/quickstart/spa/react/01-login创建了带有 auth0 的 React 应用程序

一切正常。但现在我想在我的 React 应用程序中获取用户元数据。我已经用scope字段扩展了我的 Auth0Client 构造函数,但它没有帮助。

  const auth0FromHook = await createAuth0Client({
    domain,
    client_id: clientId,
    redirect_uri: `${window.location.origin}/auth`,
    response_type: 'id_token token',
    ui_locales: 'ru',
    audience,
    scope: 'openid profile email user_metadata',
  });

我没有看到user_metadata来自 Auth0 的令牌范围: 在此处输入图像描述 我应该在哪里写scope字段以使 Auth0 返回user_metadata

PSui_locales也不行。但我在文档中看到它https://auth0.github.io/auth0-spa-js/interfaces/auth0clientoptions.html#scope

标签: javascriptreactjsauth0scopes

解决方案


你没有得到 user_metadata 因为它不是标准范围。

一种解决方法是将 user_metadata 从 Auth0 规则添加到令牌,如下所示:

// assumes you want to add this to the access token
context.accessToken['https://example.com/user_metadata'] = user.user_metadata;

有关向令牌添加自定义声明的更多信息:https ://auth0.com/docs/api-auth/tutorials/adoption/scope-custom-claims#custom-claims


推荐阅读