FirebaseUser password not updating through updatePassword() method in android

Viewed 17

I am trying to update or reset the user's password in my Android application using Firebase Authentication. For this, I am using the updatePassword() method provided by FirebaseUser class, but it is throwing the code in addOnFailureListener() and I am unable to figure out why. please help me. I have provided the code to update the password below:

updatePasswordButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String newPassword = enterPasswordEditText.getText().toString().trim();
                String confirmPassword = confirmPasswordEditText.getText().toString().trim();

                if (!newPassword.equals(confirmPassword)) {
                    Toast.makeText(EditProfileActivity.this, "Passwords don't match!", Toast.LENGTH_SHORT)
                            .show();
                } else {
                    //Updating password
                    firebaseUser.updatePassword(newPassword)
                            .addOnSuccessListener(new OnSuccessListener<Void>() {
                                @Override
                                public void onSuccess(Void unused) {
                                    Toast.makeText(EditProfileActivity.this, "Password changed successfully", Toast.LENGTH_SHORT)
                                            .show();
                                }
                            })
                            .addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    Toast.makeText(EditProfileActivity.this, "Error in changing password", Toast.LENGTH_SHORT)
                                            .show();
                                }
                            });
                }
            }
        });
1 Answers

You're getting the following error:

Error in changing password: This operation is sensitive and requires recent authentication. Log in again before retrying this request

Because changing the password is considered a sensitive operation. This means that in order to perform that operation, Firebase requires that the user has recently signed in.

If the user hasn't recently signed in, meaning that 5 minutes have already passed, when you try to perform such a sensitive operation, Firebase throws an exception that contains the error message from above.

The simplest solution for this situation would be to ask the user to re-enter their credentials and retry the operation.

Here is the official documentation for FirebaseAuthRecentLoginRequiredException.

Related