簡體   English   中英

在 Cognito 用戶池中注冊時如何檢查用戶是否已存在具有相同 email 或電話號碼的用戶

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

當新用戶使用相同的 email 和電話號碼用戶在 Cognito 用戶池中成功注冊時。 那么在 Cognito 用戶池中注冊時如何檢查用戶是否已經存在相同的 email 或電話號碼

這是我在 Cognito 用戶池中的用戶注冊代碼

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

您可以使用ListUsers API調用( https://docs.aws.amazon.com/cognito-user-identity-pools/latest/APIReference/API_ListUsers.html )按用戶的屬性過濾用戶,如果返回結果,則你可以處理用戶驗證。

您還可以在“預注冊Lambda觸發器”中包含此邏輯,以集中服務器端驗證邏輯: https//docs.aws.amazon.com/cognito/latest/developerguide/user-pool-lambda-pre -sign-up.html

我們有相同的情況。 我還需要阻止用戶使用相同的電子郵件和/或電話號碼進行注冊。

在我們的例子中,我們有一個服務User Data Service ,用於存儲用戶相關數據,包括電子郵件和電話號碼(這是與Cognito的重復數據)。

由於我們已經有了這個Service ,我更喜歡使用這個Service ,而不是獲取Cognito用戶列表並循環遍歷它們(想象一下,如果你有數千或更多的用戶,這將花費一些時間來獲取和循環時間)

我所做的是向我們的User Data Service請求一種方法,以便在已經存在電子郵件或電話號碼時有效。

我創建了一個用於進行驗證的lambda,並將lambda設置為Pre-sign up trigger

通過Google或Facebook注冊的用戶也會觸發Pre-sign up trigger ,因此如果他們的電子郵件已經存在,它將阻止他們注冊。

如果您沒有將用戶存儲在外部數據庫中,則可以像這樣捕獲 UserExists 異常:

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!"}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM