自动确认用户 Cognito + Node JS

Lui*_*dez 1 api node.js amazon-cognito

我尝试在 cognito + Node Js 应用程序中自动确认我的用户。我尝试将此代码作为 Util,并在函数内部;并且无法让它发挥作用

我尝试查看 aws 文档,但代码不明确并且没有解释太多。

这是我的代码:

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;
});
}
Run Code Online (Sandbox Code Playgroud)

谁能帮我说,我做错了什么?谢谢

小智 6

在注册回调中,您应该调用 AdminConfirmSignUp。语法如下。

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;
          });

    });
 }
Run Code Online (Sandbox Code Playgroud)