AWS Cognito 用户预注册验证。检查用户是否存在于其他数据库中

San*_*oob 5 amazon-web-services amazon-cognito aws-lambda

我目前正在将基于数据库的用户目录迁移到 AWS Cognito。我正在使用migration触发器来迁移现有用户。这工作正常。

我的问题,如果现有的电子邮件 ID 用于SignUp. 所以我想添加PreSignUp触发器来检查用户是否存在于数据库中,并为所有用户进行自动确认。

我的问题是当用户存在时如何从触发器做出响应?Cognito 期望什么样的 JSON 对象,所以在客户端它给出UserExistsException.?

代码草案。

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
Run Code Online (Sandbox Code Playgroud)

Sam*_*y93 1

在 AWS Cognito 中,当有人尝试使用现有电子邮件地址注册时,它会自动检测并引发异常。因此,您可以触发 Cognito 抛出的异常并进行处理。因此,我认为最好首先检查用户是否存在于数据库中,然后抛出自定义异常,而不继续进入 Cognito。