AdminUpdateUserAttributes是否在Cognito UserPool中创建新用户?

时间:2019-04-19 01:53:38

标签: node.js aws-sdk amazon-cognito

摘要

当我尝试更改电子邮件和email_verified时,AdminUpdateUserAttributes在Cognito UserPool中创建新用户。
我想在不验证电子邮件的情况下更新“电子邮件”属性。

代码

const provider = new AWS.CognitoIdentityServiceProvider();
provider.adminUpdateUserAttributes({
        UserPoolId: userPoolId,
        Username: username, // this is previous email
        UserAttributes: [
          {
            Name: "email",
            Value: email, // this is new email
          },
          {
            Name: "email_verified",
            Value: "true"
          }
        ]
      }, ...)

问题

电子邮件设置为用户名别名。

我想通过adminUpdateUserAttributes更新用户的电子邮件。
有时用户成功更新。
但是,我发现一些用户在电子邮件更新时重复了。
它们具有所有相同的属性值,但电子邮件和用户名(UUID)除外。

为什么会发生这种情况?以及如何解决?

更新

我在下面尝试的整个代码。

const provider = new AWS.CognitoIdentityServiceProvider();

  provider.adminGetUser({
    UserPoolId: userPoolId,
    Username: username // previous email
  }, (err, data) => {
    if (err) {
      console.log(err, err.stack)
    }else {
      const oldUsername = data.Username // get uuid username
      provider.adminUpdateUserAttributes({
        UserPoolId: userPoolId,
        Username: oldUsername,
        UserAttributes: [
          {
            Name: "email",
            Value: email, // new email
          },
          {
            Name: "email_verified",
            Value: "true"
          }
        ]
      }, (err, data) => {
          if (err) {
            console.log(err, err.stack);
          }else {
            provider.adminGetUser({
              UserPoolId: userPoolId,
              Username: email, // new email
            }, function(err, data) {
              if (err){
                console.log(err, err.stack)
              }else{
                console.log(data.Username, oldUsername)
                if (data.Username !== oldUsername){ // I would like to disable old user if they are duplicated.
                  provider.adminDisableUser({
                    UserPoolId: userPoolId,
                    Username: oldUsername
                  }, callback)
                }
              }
            })
          }
      })
    }
  })

对不起,嵌套的丑陋代码。

想法

此代码检查UUID用户名是否不同。
如果UUID不同,我认为这意味着用户已重复。
当发生这种情况时,我尝试禁用用户。

真实

即使UUID相同,也会创建新用户。

似乎AdminUpdateUserAttributes函数并不总是可以创建新用户。所以我不能每次都禁用用户。

1 个答案:

答案 0 :(得分:0)

这是预期的行为。当您更新电子邮件(在您的情况下充当用户的唯一标识符)时,它将创建一个新用户。旧用户将被禁用。如果需要,您可以添加一些其他代码以删除该旧用户。