【问题标题】:Does AdminUpdateUserAttributes create new user in Cognito UserPool?AdminUpdateUserAttributes 是否在 Cognito UserPool 中创建新用户?
【发布时间】:2019-09-09 08:36:58
【问题描述】:

总结

当我尝试更改电子邮件和 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 函数并不总是创建新用户。所以我不能每次都禁用用户。

【问题讨论】:

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


    【解决方案1】:

    这是预期的行为。当您更新电子邮件(在您的案例中充当用户的唯一标识符)时,它将创建一个新用户。老用户将被禁用。如果需要,您可以添加一些进一步的代码来删除这个旧用户。

    【讨论】:

    • 感谢您的回答。那么为什么有时会正确更新用户呢?有些情况下可以在不更改用户名(uuid)的情况下更新电子邮件。并且老用户没有被禁用。创建重复用户后仍然启用。
    猜你喜欢
    • 2020-03-19
    • 2017-08-13
    • 2020-07-03
    • 2021-02-04
    • 2018-07-27
    • 2020-10-16
    • 1970-01-01
    • 2019-09-13
    • 1970-01-01
    相关资源
    最近更新 更多