Amitjoc
Amitjoc

Reputation: 93

how to check if user already exist with same email or phone number while registration in Cognito user pool

when new user register with same email and phone number user successfully registered in the Cognito user pool. so how can i check if user already exist with same email or phone number while registration in Cognito user pool

This is my code for user registration in Cognito user pool

result = client.sign_up(                                                
            ClientId= clientId,                                                 
            Username= data['username'],                                         
            Password= data['password'],                                         
            UserAttributes=[                                                    
                {                                                               
                    'Name': 'phone_number',                                     
                    'Value': data['phone_number'],                              
                },                                                              
                {                                                               
                    'Name': 'email',                                            
                    'Value': data['email'],                                     
                },                                                              
                {                                                              
                    'Name': 'custom:usertype',                                 
                    'Value': data['userType']                                  
                },                                                             
            ],                                                                  
            ValidationData=[                                                   
                {                                                              
                    'Name': 'custom:usertype',                                 
                    'Value': 'required'                                        
                },                                                             
            ],
)

Upvotes: 1

Views: 5824

Answers (3)

Branko
Branko

Reputation: 71

If you are not storing your users in external database, you can catch an UserExists exception like this:

try:
    client.sign_up(
        ClientId = os.getenv('app_client_id'),
        Username = user.email,
        Password = user.password,
        UserAttributes = user_attributes(user)
    )
    return {"Success": f"User: {user.email} created!"}
except client.exceptions.UsernameExistsException as e:
    return {"Error": "Email already exists!"}

Upvotes: 0

Kokuou
Kokuou

Reputation: 139

We have the same scenario. I also need to prevent users to sign up with same e-mail and/or phone number.

In our case, we have a service a User Data Service which stores user related data including the e-mail and phone number (this is duplicate data with Cognito).

Since we already have this Service, I preferred using this one rather than fetching the list of Cognito Users and looping through them (Imagine if you have thousands or more of users, that would cost a time to fetch and a time to loop)

What I did is request a method to our User Data Service to valid if an e-mail or phone number already exist.

I created a lambda for doing the validation and set the lambda a Pre-sign up trigger.

Pre-sign up trigger is also triggered for users who signs up via Google or Facebook so it will prevent them to sign up if their e-mail already exists.

Upvotes: 0

Armin
Armin

Reputation: 403

You can use ListUsers API call (https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ListUsers.html) to filter users by their attributes and if it comes back with results, then you can handle user validation.

You can also contain this logic in the "Pre-Sign-up Lambda Trigger" to centralize server-side validation logic: https://docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-pre-sign-up.html

Upvotes: 1

Related Questions