【发布时间】: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