By using AWS re:Post, you agree to the Terms of Use

Can't access userAttributes of listUsersRes.Users in AWS lambda function

0

I'm filtering out unconfirmed emails in lambda function. I jsut want to access email of every user in my listUsersRes.Users. I have tried for listUsersRes.Users[0].Username it is returning username perfectly. But when I'm trying listUsersRes.Users[0].Email or listUsersRes.Users[0].userAttributes.email or listUsersRes.Users[0].request.userAttributes.email it is returning null. I have aslo AttributesToGet: ["email"].But I don't know why it is not working for email. My function:

exports.handler = async (event, context, callback) => {
    const cognitoProvider = new aws.CognitoIdentityServiceProvider({apiVersion: "2016-04-18"});
      if (event.triggerSource == "PreSignUp_SignUp" ||event.triggerSource == "PreSignUp_AdminCreateUser" || event.triggerSource=="PreSignUp_ExternalProvider") {
     try {
      
        const listUserParams={UserPoolId: event.userPoolId,AttributesToGet: ["email"],Filter: `cognito:user_status= \"${"UNCONFIRMED"}\"`, Limit: 10 };
        const listUsersRes = await cognitoProvider.listUsers(listUserParams).promise();
        
        if (listUsersRes.Users.length >= 0) {
            return callback(new Error(listUsersRes.Users[0].Username), event);//this line I'm modifying to get email attribute form listUsersRes.Users[0]
 } 
           }
     catch (error) {return callback(new Error("catch error"), event);}
    }
 else {
     var error = "This provider is not supported";
     callback(new Error(error), event);
  }
 
 };

my permission:

   "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogStream",
                "logs:PutLogEvents",
                "cognito-idp:AdminInitiateAuth",
                "cognito-idp:ListUsers",
                "cognito-idp:AdminUpdateUserAttributes",
                "cognito-idp:AdminGetUser"
            ],
1 Answer
1
Accepted Answer

You can get the email from attributes associated with the User element. https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ListUsers.html

attributes is an array of type AttributeType https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AttributeType.html

listUsersRes.Users[0].Attributes[listUsersRes.Users[0].Attributes.findIndex(p => p.Name == "email")].Value
answered 23 days ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions