Uncaught error using try-catch with async/await with Firebase reauthenticateWithCredential

Viewed 197

I'm creating a change password form using react/redux and Firebase auth. When the user enters their new password they need to enter their current password to reauthenticate first before changing password.

I am running this block of code when the user submits their info:

try {
    let current_user = firebase.auth().currentUser;
    // when there is an error here, the catch block will run, but the terminal still says uncaught error
    await current_user.reauthenticateWithCredential(firebase.auth.EmailAuthProvider.credential(inputData.email, inputData.password));
    
    if (inputData.newPassword !== inputData.confirmPassword) {
      throw { message: "Passwords do not match" };
    }
    else {
      // if I put the above await function here, the issue goes away
      await current_user.updatePassword(inputData.newPassword);
    }

  } catch (err) {
    console.log(err.message)
  }

When there is an error with reauthenticateWithCredential (eg: they entered the wrong current password), my program successfully runs the console.log(err) and displays the error, but in the console I still get an uncaught error, like this

enter image description here

However when I place the await current_user.reauthenticateWithCredential(...) under the else statement the uncaught error goes away and the code runs as intended.

What is the issue with putting the await function outside of the conditional block? Why did moving it inside the else fix the issue? How come there was an uncaught error?

0 Answers
Related