lousybear
lousybear

Reputation: 451

Trying to sign up in AWS cognito using a nodejs server usign JSON POST request

Trying to sign up into AWS Cognito using email as username attribute Getting an error message : "User email should be empty or same as username, since username attribute is email." Tried making it blank as well adding a same username attribute in JSON no fix yet.

defaults: async (Username) => {
        const definitions = {
            Pool: new AmazonCognitoIdentity.CognitoUserPool({
                UserPoolId: 'xxxxxxxxxxxxxxxx',
                ClientId: 'xxxxxxxxxxxxxxxx',
            })
        };
        if (Username) {
            definitions.cognitoUser = new AmazonCognitoIdentity.CognitoUser({ Username, Pool: definitions.Pool });
        }
        return definitions;
    }
const formData = req.body;
        const mail = formData.email;
        const { Pool } = await module.exports.defaults();
        const attributeList = [];
        const general = ['email', 'phone_number', 'name'];
        const custom = ['company_name', 'country', 'last_name', 'city', 'role'];
        const must = ['picture', 'profile', 'locale', 'birthdate', 'address'];
        general.map(g => {
            attributeList.push(new AmazonCognitoIdentity.CognitoUserAttribute({
                Name: g,
                Value: formData[g].toUpperCase()
            }));
        });
        must.map(m => {
            attributeList.push(new AmazonCognitoIdentity.CognitoUserAttribute({
                Name: m,
                Value: ''
            }));
        });
        custom.map(c => {
            attributeList.push(new AmazonCognitoIdentity.CognitoUserAttribute({
                Name: `custom:${c}`,
                Value: formData[c].toUpperCase()
            }));
        });
        if (validEmail.email(mail)) {
            Pool.signUp(formData.email, formData.password, attributeList, null, (err, result) => {
                if (err) {
                    res.status(500).send({ message: err.message });
                    return;
                }
                cognitoUser = result.user;
                res.send({ message: cognitoUser.getUsername() + " " + "successfully registered" });
            });
        }

    },

format of the JSON im sending in POST

{
    "name": "ashish",
    "email": "[email protected]",
    "phone_number": "+918249167823",
    "company_name": "Microsoft",
    "country": "INDIA",
    "last_name": "kumar",
    "city": "BANGALORE",
    "role": "COUNTRY EXECUTIVE",
    "password": "Ashish@123"
} 

Upvotes: 2

Views: 308

Answers (1)

user15878294
user15878294

Reputation:

while setting up cognito user pool, you get the option to set your username attribute. check that if its set to email id.

Upvotes: 1

Related Questions