简体   繁体   中英

How to set the Sort Key(Secondary key) in AWS Cognito?

I'm using Cognito User Pool for my iOS App User Registration. In general, when Registering a user with Cognito I'm using the email as userID. And also I'm collecting other info like Phone number, Business Name and etc. In this case when I try to register with the same email id with a Different Business name it will show an alert like User already Exist.

In my new Work case, I want to save/register the same email with a different Business name. How can I achieve it?

for example, if we are using a DynamoDB table we have the Partition key and Sort key. By using those we set the email as the Partition key and the Business Name as the Sort key and we can achieve uniqueness.

can we implement the same using Cognito? Does Cognito support the Partition key and Sort key concept? Is there any way to achieve this by using Cognito? Please help me with this issue.

Let start from this link:

Configuring User Pool Attributes

You can have changeable standard attributes as far as they are not required. You can add custom attributes as well but they are un-changeable.

Well, let's move on another case on some projects I have a need to storing a user federated identity id (ie ap-northeast-1:3c2f5c30-0dc8-4d74-91a8-bf5c688abcde) into a cognito user pool attribute. I should store it on a custom attribute (ie custom:identity_id) because of it will never change in the future.

Back to your case, as it will be dynamic values where users has ability to change their organization list so you can utilize an unused standard attributes for. For example, I will use "zoneinfo" although it looks strange to use unassociated attribute because there is no one with the name "organization". However at least users can pull their organization from their token once logged-in as like:

"zoneinfo": "[org1, org2, org3, etc]"

But it can't full accomodate your case as it should be stored after user registration. While if you set the "zoneinfo" on required registration, it must be unchangeable then. To solve this problem, you can utilize the cognito user pool Post-Confirmation trigger to run some logic to init a standard attribute with empty organization list (ie "zoneinfo": "[]") adminUpdateUserAttributes . That so users can modify this attribute then because of it is not required attribute.

Sample of adminUpdateUserAttributes:

async function updateOrgAttr() {
  try {
    var params = {
      UserAttributes: [ /* required */
        {
          Name: 'zoneinfo', /* required */
          Value: '[]'
        }
      ],
      UserPoolId: 'ap-northeast-1_xxxxxxxx', /* required */
      Username: event.userName /* event.userName is an item of post-confirmation trigger event source */
    };
    let cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
    await cognitoidentityserviceprovider.adminUpdateUserAttributes(params).promise()
  } catch(e) {
    throw e
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM