skip to Main Content

I saw few similar questions but any of the solutions didn’t help me.So basically i want to change my email and password in authentication when the user is already logged.

I tried to use the methods updateEmail/password but its didn’t changed anything.
Here example of code that i wrote:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

user.updateEmail(NewEmail)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    Log.d(TAG, "User email address updated.");
                }
            }
        }); 

(the variable ‘NewEmail’ its the email that the user decide to change and then its should to change the email in authentication)
The problem is in that case that its show me line on the ‘updateEmail’ like its tell me that the method is not work of something like that,and all what i tried didn’t worked.
And about how to change the password i tried the same way(the code from the site of firebase authentication) it was without the line on ‘updatepassword’ like in the email method,but its didn’t changed the password and when the user need to log in again its required the previous password(the first password that the user registered with).
If someone has any solution please help me!Thanks:)

2

Answers


  1. If the task is not successful, there’s an exception in there that explain why it failed – but your code is not handling that case. To figure out the root cause of the problem:

    public void onComplete(@NonNull Task<Void> task) {
        if (task.isSuccessful()) {
            Log.d(TAG, "User email address updated.");
        }
        else {
            Log.e(TAG, "User email address update failed", task.getException());
        }
    }
    
    Login or Signup to reply.
  2. If you’re seeing a line through the updateEmail method in your IDE (like Android Studio), it typically indicates that the method is deprecated. However, as of my last update, updateEmail is a valid method for a FirebaseUser in Firebase Authentication. The line might instead suggest an error or a warning. Ensure that you’re using the latest version of the Firebase Authentication library in your build.gradle file:

    implementation 'com.google.firebase:firebase-auth:latest_version'
    

    Replace latest_version with the actual latest version number. You can find this on the Firebase documentation or the Firebase section on MVNRepository.

    Ensure that your user is indeed logged in before calling updateEmail, as it requires a recent login. If the user’s authentication state is too old, you might need to re-authenticate the user before making this call.

    Updating Password

        AuthCredential credential = EmailAuthProvider
            .getCredential("[email protected]", "password1234"); // The current email and password
    
    user.reauthenticate(credential)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    // Now try to update the email or password
                }
            }
        });
    

    Re-authentication

    AuthCredential credential = EmailAuthProvider
            .getCredential("[email protected]", "password1234"); // The current email and password
    
    user.reauthenticate(credential)
        .addOnCompleteListener(new OnCompleteListener<Void>() {
            @Override
            public void onComplete(@NonNull Task<Void> task) {
                if (task.isSuccessful()) {
                    // Now try to update the email or password
                }
            }
        });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search