【问题标题】:Auth0 getProfile does not return user_idAuth0 getProfile 不返回 user_id
【发布时间】:2018-03-01 13:35:20
【问题描述】:

我正在为我的 nodejs 项目调用 node-auth0 sdk。

auth0.getProfile(accessToken, function (err, userInfo) {
    if (err) {
        res.status(400).json(err)
    }
    console.log(userInfo);
});

返回的 userInfo 不包含 user_id

如果我想通过access token获取user_id怎么办?

【问题讨论】:

  • 如果我的回答能帮助您解决问题,请您确认一下吗? :)

标签: node.js auth0


【解决方案1】:

在您的屏幕截图中,sub 声明(主题)是user_id。请确认您预期的user_id 是否应该是facebook|1

您需要使用

解析字符串 JSON 对象
const userId = JSON.parse(userInfo)['sub'];

这是工作示例:

var AuthenticationClient = require('auth0').AuthenticationClient;

var auth0 = new AuthenticationClient({
  domain: '{TENANT}.auth0.com',
  clientId: '{client id}'
});

const accessToken = '{access token}';

auth0.getProfile(accessToken, function (err, userInfo) {
  const userId = JSON.parse(userInfo)['sub'];
  console.log(userId);
});

仅供参考 - 因为您正在处理一个 String 对象,所以当您调用 sub 时,您将获得对已弃用的 String sub prototype function 的引用可以理解这种混淆。如果你不确定 Object 是什么类型,一个很好的检查方法是console.log(typeof userInfo);

【讨论】:

  • 它在sub 属性下显示user_id 值。但是,当我执行let yellerID = userInfo.sub; 时,它给了我function sub() { [native code] },它被分配给yellerID 变量。
  • 如果我这样做let yellerID = userInfo.sub(),它会给我<sub>{the object on the screenshot}</sub>
  • 我正在使用 auth0 2.7.0
  • 是的,我正在关注此文档:auth0.github.io/node-auth0/index.html
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-09
  • 2016-10-21
  • 1970-01-01
  • 2018-09-12
  • 2017-12-21
  • 2020-09-24
  • 1970-01-01
相关资源
最近更新 更多