In the latest version of Firebase you need to get the latest authentication from Firebase to change the password (for which you have to request re-authentication from Firebase), if the authentication is valid and up to date, then you can change the password you have, here I include the implementation code to change password.
In Kotlin:
private fun changePassword(email: String, oldPassword: String, newPassword: String) {
val user = FirebaseAuth.getInstance().currentUser
user?.let {
val credential = EmailAuthProvider.getCredential(email, oldPassword)
it.reauthenticate(credential).addOnCompleteListener { task ->
if (task.isSuccessful) {
it.updatePassword(newPassword).addOnCompleteListener { task ->
if (task.isSuccessful) {
Toast.makeText(this, "Password changed successfully", Toast.LENGTH_SHORT).show()
} else {
Toast.makeText(this, "Error changing password: ${task.exception?.message}", Toast.LENGTH_SHORT).show()
}
}
} else {
Toast.makeText(this, "Error reauthenticating: ${task.exception?.message}", Toast.LENGTH_SHORT).show()
}
}
} ?: Toast.makeText(this, "No user is currently logged in", Toast.LENGTH_SHORT).show()
}
In Java:
private void changePassword(String email, String oldPassword, String newPassword) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
AuthCredential credential = EmailAuthProvider.getCredential(email, oldPassword);
user.reauthenticate(credential).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
user.updatePassword(newPassword).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(getApplicationContext(), "Password changed successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Error changing password: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
} else {
Toast.makeText(getApplicationContext(), "Error reauthenticating: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
} else {
Toast.makeText(getApplicationContext(), "No user is currently logged in", Toast.LENGTH_SHORT).show();
}
}