search amazon cognito users by custom attribute

Viewed 7872

I'm developing a react native app which uses aws-amplify to interact with amazon cognito. In my use case, I need to ensure value of a custom attribute is unique across all accounts. aws-amplify does not seem to have an API which allows me to search across all accounts by a custom attribute. How can I do this?

2 Answers

Unfortunately you simply can't using Cognito alone:

https://docs.aws.amazon.com/cognito/latest/developerguide/how-to-manage-user-accounts.html#cognito-user-pools-searching-for-users-using-listusers-api

Custom attributes are not searchable. This is because only indexed attributes are searchable, and custom attributes cannot be indexed.

Your only option will be to keep your own database relating the user to the attribute value, and enforce uniqueness there. For example in DynamoDB you would record the user's sub (unique Cognito identity) and the value of the custom attribute as sort/partition keys accordingly, ensure that normal cognito clients could only read the custom attribute, and provide your own API (lambda + api-gateway) to attempt to add/update the table and use the cognito Admin API to update the 'read-only' custom attribute on the user.

That's correct you can't search custom attribute. Why can't you use one of standard attribute of AWS Cognito to be populated same as custom attribute. Just a thought. Then you can search also to check it uniqueness.

Standard attributes:

username (case-sensitive)

email

phone_number

name

given_name

family_name

preferred_username

cognito:user_status (called Status in the Console) (case-insensitive)

status (called Enabled in the Console) (case-sensitive)

sub

Related