firebase - 在flutter中更改firebase用户的密码

标签 firebase flutter firebase-authentication

我想更改 Firebase 帐户的密码。为此,用户应先输入旧密码。如果正确,则允许他更改传球。我使用的这段代码仅用于更改通行证,而不是比较旧的通行证。

void _changePassword(String password) async{
//Create an instance of the current user. 
FirebaseUser user = await FirebaseAuth.instance.currentUser();

//Pass in the password to updatePassword.
user.updatePassword(password).then((_){
  print("Successfully changed password");
}).catchError((error){
  print("Password can't be changed" + error.toString());
  //This might happen, when the wrong password is in, the user isn't found, or if the user hasn't logged in recently.
});
}

最佳答案

这个想法是通过使用 firebaseAuth 退出用户来验证旧密码,您可以通过将 user.email 传递给字符串来获取用户电子邮件,并让用户输入旧密码。如果登录过程失败,则不应更改密码。

void _changePassword(String password) async {
    FirebaseUser user = await FirebaseAuth.instance.currentUser();
    String email = user.email;

    //Create field for user to input old password

    //pass the password here
    String password = "password";
    String newPassword = "password";

    try {
      UserCredential userCredential = await FirebaseAuth.instance.signInWithEmailAndPassword(
          email: email,
          password: password,
      );
      
      user.updatePassword(newPassword).then((_){
        print("Successfully changed password");
      }).catchError((error){
        print("Password can't be changed" + error.toString());
        //This might happen, when the wrong password is in, the user isn't found, or if the user hasn't logged in recently.
      });
    } on FirebaseAuthException catch (e) {
      if (e.code == 'user-not-found') {
        print('No user found for that email.');
      } else if (e.code == 'wrong-password') {
        print('Wrong password provided for that user.');
      }
    }
  }

关于firebase - 在flutter中更改firebase用户的密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66505004/

相关文章:

firebase - 如何从 Cloud Function 访问 Cloud Firestore?

dart - flutter : setState() is not working properly

flutter - 更改本周第一天的日期时间格式

mongodb - 是否可以在 Stitch Custom Authentication/Stitch Auth 中使用 Firebase signInWithEmailAndPassword

web-applications - 更改 Google 帐户选择器显示的域

angularjs - 消息数组的 Firebase 安全规则

Node.js:如何通过uid在firebase身份验证中查找用户?

android - 在 updateChildren 中断功能后立即清除 HashMap

flutter - showMaterialBanner 不会隐藏在更改路线上查找停用的小部件的祖先是不安全的

Firebase:实现切换/添加帐户功能