amazon-cognito - 如何在 amazon-cognito 上保证 preferred_username 的唯一性?

标签 amazon-cognito

我正在使用单个表将所有数据存储在 dynamodb 中:

<表类="s-表"> <头> 分区键 (PK) 排序键(SK) 属性 <正文> 用户#gijoe 简介#gijoe { 名字:“G.I”,姓氏:“Joe” } 用户#gijoe CARD#first-card { 名称:“国王”,图片:“./king.png” }

我使用 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/

相关文章:

amazon-web-services - 如何将 Cognito 用户池设置导出到 CloudFormation 模板?

single-sign-on - AWS 认知 : support of SSO IdP-initiated workflow

amazon-web-services - Cognito 使用哪个 AWS JS SDK 包?

amazon-web-services - 我可以在没有用户名的情况下使用电子邮件或电话号码注册密码并在以后为它们设置别名吗

reactjs - AWS Cognito CompleteNewPasswordChallenge 调用 onFailure 方法,但用户在 AWS 控制台中得到确认

c# - 如何使用 AWS cognito 在 .net 上使用忘记密码自定义模板?

javascript - AWS Cognito 注册方法上的异步执行错误

amazon-web-services - 从通过 Cognito 生成的授权 token 识别 AWS Lambda 中的用户

reactjs - 尝试请求 cognito 时没有用户池

amazon-web-services - 在 Cognito 内置登录页面中显示用户名以外的内容