Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Amazon-cognito-identity-js, getting callback.newPasswordRequired is not a function error

Amazon-cognito-identity-js, getting "callback.newPasswordRequired is not a function" error while trying to change the password of an authenticated user

const poolData = {
  UserPoolId: 'eu-central-1_XXXXXXX'
  ClientId: '2xxxxxxxxxxxxxxxxxxo',
}
const authenticationData = {
  Username: name,
  Password: oldPassword,
}
const authenticationDetails = new AuthenticationDetails(authenticationData)
const userPool = new CognitoUserPool(poolData)
const cognitoUser = new CognitoUser({ Username: name, Pool: userPool })

cognitoUser.authenticateUser(authenticationDetails, {
  onSuccess(result) {
    cognitoUser.changePassword(oldPassword, newPassword, function (err, passwordChangeResult) {
      if (err) {
        console.warn(err.message || JSON.stringify(err))
      }
      console.warn(`Password change result: ${passwordChangeResult}`)
    })
  },
  onFailure(err) {
    console.warn(err)
  },
})
return null
}
like image 965
Deepak Negi Avatar asked Feb 16 '26 15:02

Deepak Negi


1 Answers

It seems that Cognito is returning a newPasswordRequired callback which you have not defined. I would assume the user was created by an Admin API call and not through the Sign Up call. Then upon signing in you are prompted to reset the password.

Taken from here [1], you can implement the newPasswordRequired callback like so.

cognitoUser.authenticateUser(authenticationDetails, {
        onSuccess: function (result) {
            // User authentication was successful
        },

        onFailure: function(err) {
            // User authentication was not successful
        },
 
        mfaRequired: function(codeDeliveryDetails) {
            // MFA is required to complete user authentication.
            // Get the code from user and call
            cognitoUser.sendMFACode(mfaCode, this)
        },
 
        newPasswordRequired: function(userAttributes, requiredAttributes) {
            // User was signed up by an admin and must provide new
            // password and required attributes, if any, to complete
            // authentication.
 
            // the api doesn't accept this field back
            delete userAttributes.email_verified;
 
            // store userAttributes on global variable
            sessionUserAttributes = userAttributes;
        }
    });

[1] https://www.npmjs.com/package/amazon-cognito-identity-js

like image 75
callo Avatar answered Feb 18 '26 12:02

callo