auto confirm users Cognito + Node JS

I trying to auto confirm my users in cognito + node Js app. I tried this code as a Util, and here inside the function; and cant make it work

I try to go to aws documentation but the code is ambiguous and doesn't explain to much.

here is my code:

userPool.signUp(req.body.email, req.body.password, attributeList, 
null, function (err, result) {
  event = {
    request: {
      "userAttributes": {
        "email": req.body.email
      },
      "validationData": {
        "Name": "email",
        "Value": req.body.email
      }
    },
    response: { 
      autoVerifyEmail: true
    }
  }
  // Confirm the user

  // Set the email as verified if it is in the request
  if (event.request.userAttributes.hasOwnProperty("email")) {
      event.response.autoVerifyEmail = 'true';
  }

  // Return to Amazon Cognito
  callback(null, event);

if (err) {
  console.log("Error aws: ", err.message);
  // return;
}
cognitoUser = result.user;
console.log('user name is ' + cognitoUser.getUsername());
next();
// return;
});
}

can anyone help me to say, what Im doing wrong? thanks

Upvotes: 1

Views: 7085

Answers (1)

Asad Mehmood
Asad Mehmood

Reputation: 514

in the signup callback you should call AdminConfirmSignUp. the syntax is given below.

https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_AdminConfirmSignUp.html

userPool.signUp(req.body.email, req.body.password, attributeList,
    null, function (err, result) {
        event = {
            request: {
                "userAttributes": {
                    "email": req.body.email
                },
                "validationData": {
                    "Name": "email",
                    "Value": req.body.email
                }
            },
            response: {
                autoVerifyEmail: true
            }
        }
        // Confirm the user

        var confirmParams = {
            UserPoolId: 'provide your user pool id', /* required */
            Username: req.body.email /* required */
          };
          cognitoidentityserviceprovider.adminConfirmSignUp(confirmParams, function(err, data) {
            if (err) console.log(err, err.stack); // an error occurred

           
            // Set the email as verified if it is in the request
            if (event.request.userAttributes.hasOwnProperty("email")) {
                event.response.autoVerifyEmail = 'true';
            }

            // Return to Amazon Cognito
            callback(null, event);

            if (err) {
                console.log("Error aws: ", err.message);
                // return;
            }
            cognitoUser = result.user;
            console.log('user name is ' + cognitoUser.getUsername());
            next();
            // return;
          });

    });
 }

Upvotes: 6

Related Questions