Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 995
Karan Avatar asked Feb 09 '18 06:02

Karan


People also ask

How do I get custom attribute values in Cognito?

To add a custom attribute through the consoleGo to Amazon Cognito in the AWS Management Console. If the console prompts you, enter your AWS credentials. Choose Manage User Pools. On the Your User Pools page, choose the user pool that you want to configure.

How do I edit a custom attribute in Cognito?

Short description. You can't change standard user pool attributes after a user pool is created. Instead, create a new user pool with the attributes that you want to require for user registration. Then, migrate existing users to the new user pool by using an AWS Lambda function as a user migration trigger.

How do I add custom attributes in Amplify authentication?

To configure and enable standard user attributes in your app, you can run the Amplify update auth command and choose Walkthrough all the auth configurations. When prompted for Specify read attributes and Specify write attributes, choose the attributes you'd like to enable in your app.

What is sub attribute Cognito?

Cognito sub attributeWhen creating a user Cognito will assign a generated unique IDs (the sub attribute). This attribute cannot be changed and in case you import users from another pool/backup it will change.


1 Answers

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!

like image 120
Tom Avatar answered Oct 19 '22 21:10

Tom