Firebase-重新验证后更新密码时出现问题

时间:2019-03-20 22:28:16

标签: javascript firebase firebase-authentication

我要做什么?

  1. 重新验证用户,检查当前密码(this.state.currentPassword)
  2. 更新新密码(this.state.newPassword)

我要面对什么问题?

  1. 步骤1成功执行
  2. 但是当尝试执行STEP 2时,它将引发错误并得到错误 从外部尝试,即“错误重新验证”

var user = firebaseApp.auth().currentUser;
var credential = firebase.auth.EmailAuthProvider.credential(
    firebaseApp.auth().currentUser.email,
    this.state.currentPassword
);

// Prompt the user to re-provide their sign-in credentials


// STEP 1 : re-authenticate user

user.reauthenticateAndRetrieveDataWithCredential(credential).then(function() {
  // User re-authenticated.

  //Upon re-authenticating, update password

  //STEP 2 : UPDATE PASSWORD

  var newPassword = firebase.getASecureRandomPassword();

  user.updatePassword(newPassword).then(function() {
    alert("SUCCESS")
    // Update successful.
  }).catch(function(error) {
    console.log(error)
    // An error happened.
  });


}).catch(function(error) {
  // An error happened.
  alert("Error Reauthenticating")
});

1 个答案:

答案 0 :(得分:0)

不确定是什么问题,但可以通过以下代码获得所需的输出。

贷记到this article

reauthenticate = (currentPassword) => {
  var user = firebase.auth().currentUser;
  var cred = firebase.auth.EmailAuthProvider.credential(
    user.email, currentPassword);
  return user.reauthenticateAndRetrieveDataWithCredential(cred);
}

changePassword = (currentPassword, newPassword) => {
  this.reauthenticate(currentPassword).then(() => {
    var user = firebase.auth().currentUser;
    user.updatePassword(newPassword).then(() => {
      console.log("Password updated!");
    }).catch((error) => {
      console.log(error);
    });
  }).catch((error) => {
    console.log(error);
  });
}
  

可能需要ES6语法,但明天会查询。