Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Cognito user pre-sign up validation. To check if user exist in other DB

I'm currently migrating my DB based user directory to AWS Cognito. I'm using migration trigger for migrating existing users. This working fine.

My issue, What if an existing email id is used for SignUp. So I thought to add PreSignUp trigger which checks user exists in DB and also make auto confirmation for all users.

My question is how can I make a response from trigger when a User exists ? What kinda JSON object the cognito is expecting, So that in client side it gives UserExistsException.?

Draft Code.

def handler(event, context):
    event['response']['autoConfirmUser'] = True

    if event['request']['userAttributes']['email'] is not None:
        if not user_exists(event['request']['userAttributes']['email']):
            event['response']['autoVerifyEmail'] = True
        else:
            return #Validation Error response

    return event
like image 779
Sanoob Avatar asked Nov 07 '22 05:11

Sanoob


1 Answers

In AWS Cognito it automatically detects and throws an exception when someone tries to register with an existing email address. Therefore you can trigger the exception throwing out from Cognito and do the handling. So that from my opinion it is better to first check the user is exists in your DB and throw a custom exception without proceeding in to Cognito.

like image 121
SamPiy93 Avatar answered Nov 14 '22 23:11

SamPiy93