在Flatter中更改firebase用户的密码
我想更改firebase帐户的密码。要做到这一点,用户应在输入之前输入旧密码。如果正确的话,他可以换通行证。我使用了这段代码,它只用于更改通行证,而不是比较旧的通行证在Flatter中更改firebase用户的密码,firebase,flutter,firebase-authentication,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
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将自动要求他们在更改密码(或执行其他敏感操作)之前重新验证。因此,您通常只需要在Firebase告诉您时要求用户重新验证
发生这种情况时,请按照上的文档中的步骤进行操作。因此,您希望用户在更改旧密码之前先验证它?它成功了!谢谢