如何获取 Auth0 Hooks 中的 user_id 字段?

How to get the user_id field in Auth0 Hooks?

我正在尝试 Auth0 中的一些新 Hooks 功能,特别是 post-注册挂钩。我希望使用 user_id 配置文件字段,即 here 中提到的 provider|id,这样我们就可以在应用程序端创建用户记录。我们会为我们的用户分配 UUID,但每个用户都会有一个来自 Auth0 的唯一密钥。

挂钩示例如下所示 - 我正在导出我可用的整套数据:

module.exports = function (user, context, cb) {
  var request = require('request');
  request({
    method: 'POST', 
    url: 'https://requestb.in/1ejaxvz1',
    body: {
     "id": user.id,
     "user": user,
     "context": context
    },
    json: true
  },
  function(error, response, body) { 
    console.log(user);
    console.log(context);
    cb(error, response) 
  });
};

然而,虽然这为我提供了用户的基本 ID,但似乎没有关于完整 user_id 的信息或任何关于 provider 的信息可供挂钩使用。换句话说,我没有给我看起来像 provider|12345678 的完整 ID,而是只得到 12345678。当我从 Auth0 获取 jwt 时,sub 值返回为 provider|12345678.

所以我的问题有两部分: 1)这个完整的用户 ID 是出于某种原因故意遗漏的,还是设计疏忽?在撰写本文时,此功能仍处于测试阶段。

2) 如果没有完整的用户 ID provider|12345678,我无法对 jwt 中的 sub 值进行精确匹配。但是,我 可以 尝试通过删除 provider 来部分匹配。我不清楚这是否是一种有效的做法。我怀疑不是,但这很难测试。

Post-registration hook 仅在数据库注册时为 运行,并且 user_id 格式在输入数据库时​​将始终以 auth0| 为前缀。然而,这在未来可能会改变。

1) Is this full user ID intentionally left out of hooks for a reason or is this a design oversight? This feature is still in beta at the time of writing this.

这是设计使然,但我同意更好的方法是公开完整的 user_id 挂钩,包括提供商。

2) Without the full user ID provider|12345678 I can't do an exact match on the sub value in the jwt. But, I could attempt to partial-match by dropping the provider. I'm not clear on whether this is a valid practice though. I suspect it's not, but this is tricky to test.

是的,您必须在 user_id 前加上 auth0| 前缀才能获得完全匹配。同样,这不是最佳实践,但 Hooks 仍处于测试阶段。