首页 > 解决方案 > 如何在 lambda 中读取 AWS cognito 自定义属性和登录用户

问题描述

我在 aws congnito 池中创建了一个自定义属性,现在添加 Post authentication lambda 和 lambda 内部想要读取“自定义属性”和登录用户名。

在 Node.js lambda 内部:

  var email=event.request.userAttributes.email;
  var refNumber=event.request.userAttributes.ref_number; //custom attribute
  var loginid=event.request.userAttributes.username;//loggedin id in cognito

我能够正确获取电子邮件 ID,但是登录的用户名和自定义属性都未定义。

标签: amazon-web-servicesaws-lambdaamazon-cognito

解决方案


自定义属性的名称custom:xxx在哪里xxx是您的自定义属性名称:

{
    "version": "1",
    ...,
    "userName": "...",
    "triggerSource": "PostAuthentication_Authentication",
    "request": {
        "userAttributes": {
            "sub": "...",
            "cognito:user_status": "CONFIRMED",
            ...
            "locale": "en",
            ...
            "custom:xxx": "yyy"
        },
        "newDeviceUsed": true
    },
    "response": {}
}

所以对你ref_number来说,应该是event.request.userAttributes['custom:ref_number']

用户名很简单event.userName


推荐阅读