vaquar khan
vaquar khan

Reputation: 11489

How to read AWS cognito custom attributes and logged in user in lambda

I have created a custom attributes inside aws congnito pool, now adding Post authentication lambda and inside of lambda want to read "custom attributes" and loggedin username .

Inside 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

i am able to fetch email id properly however both loggedin username and custom attribute coming undefined .

Upvotes: 2

Views: 4605

Answers (1)

jogold
jogold

Reputation: 7417

The custom attributes are named custom:xxx where xxx is your custom attribute name:

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

So for your ref_number, it should be event.request.userAttributes['custom:ref_number'].

The username is simply event.userName.

Upvotes: 9

Related Questions