Auth0 getProfile 没有 return user_id
Auth0 getProfile does not return user_id
我正在为我的 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怎么办?
在您的屏幕截图中,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 的引用 可以理解混乱。如果您不确定对象是什么类型,检查的好方法是 console.log(typeof userInfo);
我正在为我的 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怎么办?
在您的屏幕截图中,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 的引用 可以理解混乱。如果您不确定对象是什么类型,检查的好方法是 console.log(typeof userInfo);