Uncaught (in promise) TypeError: callback is undefined amazon-cognito-identity-js user.completeNewPasswordChallenge

932 Views Asked by At

all. Can anyone help me understand why I am getting a Uncaught (in promise) TypeError: callback is undefined in the below code? It does not make it to the on success or on failure clauses.

const onSubmitPasscode = (event) => {
    event.preventDefault();

    if (passwordFirst !== passwordSecond) {
      alert('the two inputs provided are not the same');
    } else {
      const user = new CognitoUser({
        Username: email,
        Pool: UserPool
      });

      const authDetails = new AuthenticationDetails({
        Username: email,
        Password: password
      });

      user.authenticateUser(authDetails, {
        onFailure: (err) => {
          console.error('onFailure:', err);
        },
        newPasswordRequired: () => {
          user.completeNewPasswordChallenge(passwordFirst, {
            onFailure: (err) => {
              console.error('onFailure:', err);
            },
            onSuccess: (result) => {
              console.log(passwordFirst);
              console.log('call result: ', result);
              navigate('dashboard');
            }
          });
        }
      });
    }
1

There are 1 best solutions below

1
On

Define an onSuccess function for authenticateUser:

user.authenticateUser(authDetails, {
  onSuccess: (result) => {
    console.log(result);
  },
  onFailure: (err) => {
    console.error('onFailure:', err);
  },
  newPasswordRequired: () => {
    user.completeNewPasswordChallenge(passwordFirst, {
      onFailure: (err) => {
        console.error('onFailure:', err);
      },
      onSuccess: (result) => {
        console.log(passwordFirst);
        console.log('call result: ', result);
        navigate('dashboard');
      }
    });
  }
}

Also, based on the documentation, newPasswordRequired takes a function whose signature is different from what you are providing. So I think what you might want is really:

user.authenticateUser(authDetails, {
  onSuccess: (result) => {
    console.log(passwordFirst);
    console.log('call result: ', result);
    navigate('dashboard');
  }
  onFailure: (err) => {
    console.error('onFailure:', err);
  },
  newPasswordRequired: (userAttributes, requiredAttributes) => {
    /*whatever you want to do when new password is required*/
  }
}