Home > database >  How to set sign up with email or phone-number option in AWS Cognito?
How to set sign up with email or phone-number option in AWS Cognito?

Time:06-27

How to set sign up with email or phone-number option in AWS Cognito?

So I would like to have a simple sign-up. I want users to sign-up with username, email/phone-number, password and repeat password. How can I configure the ability to make it that either email or phone-number is required?

CodePudding user response:

A way to require attributes conditionally is to have them as not required in the User Pool settings and define a Pre sign-up Lambda trigger function that will verify the presence of at least one of the fields at Sign up, and reject if none is provided (similar to this example):

exports.handler = (event, context, callback) => {
    // Impose a condition that the email or phone_number is provided.
    var userAttributes = event.request.userAttributes;
    if (!userAttributes.email && !userAttributes.phone_number) {
        var error = new Error("Cannot register users without email or phone number");
        // Return error to Amazon Cognito, fail sign up
        callback(error, event);
    }
    // Return to Amazon Cognito, proceed with sign up
    callback(null, event);
};
  • Related