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

馋奶兔 提交于 2021-02-08 10:46:48

问题


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'                                        
                },                                                             
            ],
)

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/55490192/how-to-check-if-user-already-exist-with-same-email-or-phone-number-while-registr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!