我正在使用单个表将所有数据存储在 dynamodb 中:
我使用 preferred_username
作为分区键的一部分,因此需要它是唯一的以避免用户数据冲突。
我如何保证我的用户池中的两个用户不能具有匹配的 preferred_username
?
编辑:
@Lukas的答案做到了。请注意,它要求我删除并重新创建我的 cloudformation 堆栈,这就是我第一次尝试失败的原因。现在,当我尝试编辑 preferred_username
时,我得到了我正在寻找的错误:
{
"message": "Already found an entry for the provided username.",
"code": "AliasExistsException",
"time": "2021-01-19T09:36:47.874Z",
"requestId": "7b52dbc2-58c5-4354-aa51-66d4dc7472a0",
"statusCode": 400,
"retryable": false,
"retryDelay": 85.84051584672932
}
最佳答案
username
在单个池中是唯一的。与别名相同。 preferred_username
可以配置为用户名别名。
https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-settings-attributes.html
要点:
Developers can use the preferred_username attribute to give users a username that they can change. For more information, see Overview of Aliases.
The username must be unique within a user pool. A username can be reused, but only after it has been deleted and is no longer in use.
You can allow your end users to sign in with multiple identifiers by using aliases.
The preferred_username attribute provides users the experience of changing their username, when in fact the actual username value for a user is not changeable. If you want to enable this user experience, submit the new username value as a preferred_username and choose preferred_username as an alias. Then users can sign in with the new value they entered. If preferred_username is selected as an alias, the value can be provided only when an account is confirmed. The value cannot be provided during registration.
....
Alias values must be unique in a user pool. If an alias is configured for an email address or phone number, the value provided can be in a verified state in only one account. During sign-up, if an email address or phone number is supplied as an alias from a different account that has already been used, registration succeeds. Nevertheless, when a user tries to confirm the account with this email (or phone number) and enters the valid code, an AliasExistsException error is thrown. The error indicates to the user that an account with this email (or phone number) already exists. At this point, the user can abandon the new account creation and can try to reset the password for the old account. If the user continues creating the new account, your app should call the ConfirmSignUp API with the forceAliasCreation option. This moves the alias from the previous account to the newly created account, and it also marks the attribute unverified in the previous account.
关于amazon-cognito - 如何在 amazon-cognito 上保证 preferred_username 的唯一性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65752457/