Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Cognito - SerializationException (Start of structure or map found where not expected)

I have created a user in Cognito user pool, using the create user option as shown in the image, and want that user to set a new password on first Log In. For this I am using below given method, as given in the AWS documents Create User from AWS

cognitouser.completeNewPasswordChallenge()

Below is my code

//Start: setNewPasswordForAdmin
    setNewPasswordForAdmin(username: string, password: string) {

        //Start: Creating new user with username and pool data
        const adminUserdata = {
            Username: username,
            Pool: userpoolAdmin
        }
        const adminuser = new CognitoUser(adminUserdata);
        //End: Creating new user with username and pool data


        //create attributelist array which will contain all the attributes requried for signing in
        //these attributes are those attributes, which are selected while creating user pool on aws
        const attributeList : CognitoUserAttribute[] = [];

        //form a json object containing Name and Value of attribute used
        const usernameattribute = {
            Name:'username',
            Value: username
        }

        //Push list of attributes in the attributeList array
        attributeList.push(new CognitoUserAttribute(usernameattribute));
        console.log(attributeList);
        const that = this;

        adminuser.completeNewPasswordChallenge(password, attributeList ,{
            onFailure(err){

                console.log(err);
            },
            onSuccess(result){
                console.log(":::::::: Password change successfull ");

                that.router.navigate(['']);
            }
        });
    }
    //End: setNewPasswordForAdmin

After executing this I get an error saying,

{code: "SerializationException", name: "SerializationException", message: "Start of structure or map found where not expected."}

These are the attributes which I have selected in User Pool

List Of Attributes and permissions in user pool

Please help me to solve this.

like image 798
Shardul Sane Avatar asked Nov 26 '22 01:11

Shardul Sane


1 Answers

In my case it ended up being malformed data being sent to my resetPassword helper. The function was looking for three parameters (username, code, password):

export default function (username, code, password) {
    …
}

… and I was sending them as an object:

yield call(authResetPassword, {
    code: DOMPurify.sanitize(verificationCode),
    username: DOMPurify.sanitize(email),
    password: DOMPurify.sanitize(newPassword),
})

I just updated the helper and I was all set!

export default function ({ username, code, password }) {
    …
}

Just log your data in the setNewPasswordForAdmin function and I’ll bet you’ll find something awry there.

like image 87
Brandon Durham Avatar answered Dec 06 '22 15:12

Brandon Durham