AWS Cognito: Methods to Get a List of users?

15,440

Solution 1

Late response but for those who will probably need it :

  1. Create a new IAM policy, with the Cognito Listusers allowed

  2. the code in JS

    const cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider({
         apiVersion: '2016-04-18',
         region: "eu-central-1",
         credentials: Auth.essentialCredentials(credentials)
       });
       var params = {
         UserPoolId: environment.amplify.Auth.userPoolId, /* required */
         AttributesToGet: [
           "email",
         ],
         Limit: 0
       };
       cognitoidentityserviceprovider.listUsers(params, function (err, data) {
         if (err) console.log(err, err.stack); // an error occurred
         else console.log(data);           // successful response
       });
     });
    

Solution 2

This function will list the users, just use the aws key and secret, user pool region and id and call the function getUsers(). You can use filters in params to do a more specific request. https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html#listUsers-property

var AWS = require('aws-sdk');

exports.getUsers= () => {
var params = {
  UserPoolId: USER_POOL_ID
  AttributesToGet: [
    'ATTRIBUTE_NAME',
  ],
};

return new Promise((resolve, reject) => {
    AWS.config.update({ region: USER_POOL_REGION, 'accessKeyId': AWS_ACCESS_KEY_ID, 'secretAccessKey': AWS_SECRET_KEY });
    var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
    cognitoidentityserviceprovider.listUsers(params, (err, data) => {
        if (err) {
            console.log(err);
            reject(err)
        }
        else {
            console.log("data", data);
            resolve(data)
        }
    })
});

}

Solution 3

Look into the Cognito Identity Service Provider SDK.

SDK Link

The function that want is listUsers();

Share:
15,440
claudioz
Author by

claudioz

Updated on June 23, 2022

Comments

  • claudioz
    claudioz almost 2 years

    I'm developing a web application using Angular 4 (with TypeScript language) front-end side, and using AWS services back-end size. This application can be only accessed by a group of users (each has its own mail and password). This group of users is defined in AWS Cognito - User Pool. How can I have the entire list of these users with their properties to see her in the frontend? And how do I change their properties only frontend side?

    I saw the JavaScript methods for AWS Cognito (https://github.com/aws/amazon-cognito-identity-js), but I didn't find anything for what I want to do.

    I would like to use some method to show the full list of users with their properties (as seen in the AWS mangement console of Cognito) and that always, thanks to another method, my application it's able to edit some information as a "group" of a user.

    Can anyone tell me if this is possible?