0
votes

I'm trying to use the firebase changePassword() function together with the error call back.

changePassword: function(email, oldPassword, newPassword){
      return auth.$changePassword(email, oldPassword, newPassword).then(function(error) {
console.log(error);
});

The script is effective and works. However the error callback does not. When the wrong oldpassword is entered, for example, nothing happens and nothing is logged to the console. When everything is done correctly, and the password is successfully changed, error is logged to the console and would appear to be undefined.

Anyone having a similar issue / know what I'm doing wrong?

Thanks

1
This is AngularFire, not Firebase. What version is this? - Kato

1 Answers

2
votes

AngularFire's $changePassword method returns a promise. When working with Futures/promises, the standard is unlike node.js-esque callbacks in that the error is not returned in the success callback, but instead passed to the failure callback (a second function passed into then).

You can also utilize the special final and catch methods as shortcuts for then(success, failure). This allows for chaining sequential events in sophisticated ways:

stepOne()
  .then(stepTwo)
  .then(stepThree)
  .then(stepFour)
  .catch(function(error) {
     console.error(error);
  });

Note how we only handle the errors in exactly one place. Each "step" would only be triggered if the previous step succeeds, so we don't need to constantly include if/then/else logic to handle failures at every point.

So, to translate, the API is utilized like this:

$changePassword(...).then(/* success */, /* failure */);

Or in your case:

changePassword: function(email, oldPassword, newPassword){
      return auth.$changePassword(email, oldPassword, newPassword).catch(function(error) {
console.log(error); });
   // or...
   // return auth.$changePassword(...).then(null, function(error) { ... });
});