Search code examples
amazon-web-servicesamazon-cognitosample

How do I fix the AWS Wild Rydes sample error: InvalidParameterException: "username should be an email"


Following the AWS WildRydes sample to Build a Serverless Web Application, after setting up the Cognito User Pool any attempt to register a user on the sample website generates the error:

 InvalidParameterException: Username should be an email

The username I am using is a valid email (my genuine email address). The error is being returned from the Cognito service. Google turns up nothing obvious. I have followed the sample carefully.


Solution

  • It seems obvious that Cognito is attempting to validate the username passed and finding that it is not a valid email address, despite me supplying a valid email. Cognito is unlikely to be buggy, so I dug into the sample code supplied by AWS. In the js/cognito-auth.js file I found that where the username parameter is being passed to Cognito it is wrapped in a toUsername() function:

    userPool.signUp(toUsername(email), password, [attributeEmail], null,
    

    The function replaces the @ in the email with -at-:

    function toUsername(email) {
        return email.replace('@', '-at-');
    }
    

    I took the quick way out and updated the toUsername() function:

    function toUsername(email) {
        return email;
    }
    

    After pushing the change to CodeCommit and waiting for Amplify to deploy it, my registration and Sign in works. The sample code I changed was straight from AWS, so I'm not sure how this crept in, could be:

    1. I made a mistake with the user pool setup, though I was pretty careful and it's all the defaults
    2. Something has changed with the default setup of a User Pool
    3. Cognito has added validation forcing the user name to be a email.

    Anyway, I'm posting this to assist if others experience it.