Karan
Karan

Reputation: 455

Custom attributes missing in the getUserAttributes response - AWS cognito

const userData = { Username: username, Pool: userPool };
const cognitoUser = new AWS.CognitoIdentityServiceProvider.CognitoUser(userData);
cognitoUser.getUserAttributes(function(err, result) {
                        if (err) {
                            alert(err);
                            return;
                        }
                        console.log(result)
                        for (i = 0; i < result.length; i++) {
                            console.log('attribute ' + result[i].getName() + ' has value ' + result[i].getValue());
                        }
                    });

I have a custom field userType. I have set the read, write permission for the custom attribute. But in the response of getUserAttributes i am getting only standard attributes(sub, email_verified, email). How can i retrieve both the standard and custom attributes?

This is my response

enter image description here

Upvotes: 10

Views: 7535

Answers (1)

Tom
Tom

Reputation: 452

First of all, you need to make sure the App client has permissions to read the attribute(s).

Go to AWS Cognito, select the User Pool, then General Settings > App clients and select the App Client, then click "Set attribute read and write permissions"

You should see the following options (notice I have "custom:premium" attribute created and permission to read set)

Then in order to see the custom attributes in cognitoUser.getUserAttributes() results you need to set some value to it, this can be done for example via AWS CLI with the following command:

aws cognito-idp admin-update-user-attributes \
--user-pool-id userPoolId \
--username userName \
--user-attributes Name=custom:premium,Value=1

In the example above I'm setting "custom:premium" attribute to value 1 (obviously you need to replace userPoolId and userName with correct values.

  • userPoolId can be found in AWS Cognito (for example: eu-central-1_896ERRASw)
  • userName is username of the user you are currently logged in with in your app (and invoking cognitoUser.getUserAttributes())

    And here is the result of the function call after properly setting permission and value of the custom attribute.

Hope it helps!

Upvotes: 23

Related Questions