3

I want for my backend server (node.js) make a call through aws-sdk library to see if exists a user with specific mail. Is there a proper method to do this or a work arround without using user's credentials to do this procedure?

  • 3
    Assuming your backend is able to do authenticated requests to the AWS Api, there is a `listUsers` methods. This lists all users of a userpool. This can even be filtered for instance by email ... https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CognitoIdentityServiceProvider.html#listUsers-property – derpirscher Sep 10 '21 at 15:45

1 Answers1

1

Yes this can be done the listUsers() AWS Javascript CognitoIdentityServiceProvider() API.

Call listUsers() to check if user exists

const cognito = new AWS.CognitoIdentityServiceProvider();

async function isEmailRegistered(email) {
   //Check if user email is registered

   var params = {
     UserPoolId: 'eu-west-1_3bqeRjkSu', /* required */
     AttributesToGet: [
       'email',
     ],
     Filter: "email = \"" + email + "\"",
   };

   return cognito.listUsers(params).promise();
}

Call and handle result

await isEmailRegistered(qsp.email).then( data => {

  if (data.Users.length === 0) {
    //User does not exist
  } else {
    //User does exist
  }
}).catch(err => {
  console.log("error: " + JSON.stringify(err))
});
Zog
  • 1,094
  • 2
  • 11
  • 24