我想更改 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/