使用Firebase重新验证

时间:2016-10-05 11:35:05

标签: javascript firebase firebase-authentication angularfire2

我非常感谢有关如何在Firebase中重新验证用户的帮助。如果文档没有解释如何使用它,我想知道添加所有这些强大的功能是否有任何意义:

目前,这是我正在尝试的,而且它无法正常工作。错误为cannot read property 'credential' of undefined

在构造函数中:

  constructor(@Inject(FirebaseApp) firebaseApp: any) {
    this.auth = firebaseApp.auth();
    console.log(this.auth);
  }

然后是函数

changePassword(passwordData) {
    if(passwordData.valid) {
      console.log(passwordData.value);
      // let us reauthenticate first irrespective of how long
      // user's been logged in!

      const user = this.auth.currentUser;
      const credential = this.auth.EmailAuthProvider.credential(user.email, passwordData.value.oldpassword);
      console.log(credential);
      this.auth.reauthenticate(credential)
        .then((_) => {
          console.log('User reauthenticated');
          this.auth.updatePassword(passwordData.value.newpassword)
            .then((_) => {
              console.log('Password changed');
            })
            .catch((error) => {
              console.log(error);
            })
        })
        .catch((error) => {
          console.log(error);
        })
    }
  }

6 个答案:

答案 0 :(得分:11)

firebase.User上调用reauthenticate()方法,而不是firebase.auth.Auth本身。

var user = firebase.app.auth().currentUser;
var credentials = firebase.auth.EmailAuthProvider.credential('puf@firebaseui.com', 'firebase');
user.reauthenticate(credentials);

更新(2017年7月)

Firebase Web SDK 4.0版本有一些重大变化。来自release notes

  

BREAK:firebase.User.prototype.reauthenticate已被删除,转而使用firebase.User.prototype.reauthenticateWithCredential

据我所知,reauthenticateWithCredential是旧方法的替代品。

答案 1 :(得分:1)

这里有一些代码使用户能够(a)在Firebase中重新认证,并且(b)在为我重新认证后更改其密码。在撰写本文时,我研究了大约一个小时,因此希望可以节省一分钟。

写在VueJS中:

changePassword() {
            let self = this; // i use "self" to get around scope issues
            var user = firebase.auth().currentUser;
            var credential = firebase.auth.EmailAuthProvider.credential(
                this.$store.state.userId, // references the user's email address
                this.oldPassword
            );

            user.reauthenticateWithCredential(credential)
                .then(function() {
                    // User re-authenticated.
                    user.updatePassword(self.newPassword) 
                        .then(function() {
                            console.log("Password update successful!");
                        })
                        .catch(function(error) {
                            console.log(
                                "An error occurred while changing the password:",
                                error
                            );
                        });
                })
                .catch(function(error) {
                    console.log("Some kinda bug: ", error);
                    // An error happened.
                });

答案 2 :(得分:0)

截至2019年5月的细微变化,请查看更多详细信息here。代码如下:

var user = firebase.auth().currentUser;
var credential = firebase.auth.EmailAuthProvider.credential(user.email, password);

// Prompt the user to re-provide their sign-in credentials
return user.reauthenticateWithCredential(credential);

答案 3 :(得分:0)

直接在 changeEmail("new email","password") 中调用 onPressed 以更新用户电子邮件,无需重新验证错误

RaisedButton(
  onPressed: () {
    changeEmail(_emailController.text, _passwordController.text);
  }              

 Future<void> changeEmail(String email, String password) async {
   User user = await FirebaseAuth.instance.currentUser;
  print(email);
  print(password);
  try {
    try {
      var authResult = await user.reauthenticateWithCredential(
        EmailAuthProvider.getCredential(
          email: user.email,
          password: password,
        ),
      );
      user.updateEmail(email).then((_) {
        print("Succesfull changed email");
        _backthrow();
      }).catchError((error) {
        showAlertDialog(context, error.message);
        print("email can't be changed" + error.toString());
      });
      return null;
    } catch (e) {
      print("2");
    }
  } catch (e) {
    print(e.message);
    showAlertDialog(context, e.message);
  }
}

答案 4 :(得分:0)

这是一个如何使用 Firebase 重新进行身份验证的完整示例

var pass = "abcdefg";
var user = firebase.auth().currentUser;
var credential = firebase.auth.EmailAuthProvider.credential(user.email, pass);

user.reauthenticateWithCredential(credential).then(() => {
    console.log("Its good!");
}).catch((error) => {
    console.log(error);
});

答案 5 :(得分:-1)

保存主电子邮件时出现重新认证错误run_id
我不知道如何实现该文献记录不充分的import azureml.core from azureml.core.experiment import Experiment from azureml.core.workspace import Workspace froom azureml.train.automl.run import AutoMLRun ws = Workspace.from_config() experiment_name = 'YOUREXPERIMENTNAME' experiment = Experiment(ws, experiment_name) run_automl = AutoMLRun(experiment, run_id="YOUR RUN ID") best_run, fitted_model = remote_run.get_output() 方法,因此,我只是注销了用户并重定向到登录页面。这是一个hack,但它就像魅力一样!

auth/requires-recent-login