Rsquare
Rsquare

Reputation: 16

Firebase user's email address method updateEmail is not working correctly

I also used the re-authentication method before using before the updateEmail method. Everything working correctly. Even the toast message in the updateEmail also appears as expected but there is no change in the firebase database of the user.

  1. It detects the already existed email in the firebase database and show a toast of "Email already exist".
  2. It also works fine in checking the email from firebase and if it doesn't collide then it shows a toast message "Email upadated".
  3. But still it doesn't change the email in the firebase database.

    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    
        // Get auth credentials from the user for re-authentication. The example below shows
        // email and password credentials but there are multiple possible providers,
        // such as GoogleAuthProvider or FacebookAuthProvider.
    AuthCredential credential = EmailAuthProvider
            .getCredential(mAuth.getCurrentUser().getEmail(), password);
    
        // Prompt the user to re-provide their sign-in credentials
    user.reauthenticate(credential)
            .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
    
                        if (task.isSuccessful()) {
                            Log.d(TAG, "User re-authenticated.");
                            mAuth.fetchSignInMethodsForEmail(email.getText().toString()).addOnCompleteListener(new OnCompleteListener<SignInMethodQueryResult>() {
                                @Override
                                public void onComplete(@NonNull Task<SignInMethodQueryResult> task) {
    
                                    if (task.isSuccessful()) {
    
                                        try {
    
    
                                            if (task.getResult().getSignInMethods().size() == 1) {
                                                Log.d(TAG, "onComplete: This will return the signin methods");
                                                Toast.makeText(getActivity(), "The email is already exist", Toast.LENGTH_SHORT).show();
    
    
                                        }else{
                                            Log.d(TAG, "onComplete: Email is not present. User can change it");
                                            FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    
                                            user.updateEmail(email.getText().toString())
                                                    .addOnCompleteListener(new OnCompleteListener<Void>() {
                                                        @Override
                                                        public void onComplete(@NonNull Task<Void> task) {
                                                            if (task.isSuccessful()) {
                                                                Log.d(TAG, "User email address updated.");
                                                                Toast.makeText(getActivity(), "The email updated.", Toast.LENGTH_SHORT).show();
    
                                                            }
                                                        }
                                                    });
    
                                        }
                                    }catch(NullPointerException e) {
                                            Log.e(TAG, "onComplete: NullPointerException" + e.getMessage());
                                        }
                                }
    
                                    }
    
    
                            });
    
    
                        } else {
                            Log.d(TAG, "onComplete: User re-authentication failed.");
                        }
    
    
                }
            });
    

Upvotes: 0

Views: 1955

Answers (1)

user10151289
user10151289

Reputation:

This mAuth.getCurrentUser().getEmail() will give you the email from the Firebase built-in user class and not from the database itself.

And I cant see any way that you have changed the email in the database itself.

This function updates the email in the Firebase user class which can be seen in the "Authentication" section of your Firebase console

user.updateEmail(email.getText().toString())
             .addOnCompleteListener(new OnCompleteListener<Void>() {
              @Override
               public void onComplete(@NonNull Task<Void> task) {
                      if (task.isSuccessful()) {
                        Log.d(TAG, "User email address updated.");
                        Toast.makeText(getActivity(), "The email updated.", Toast.LENGTH_SHORT).show();

                     }
                 }
           });

To update the data in database you have to follow this

Once again I am repeating you are updating the email of a user in the Firebase user class not the actual database. As mentioned, updated email will only be visible in "Authentication" section of Firebase console and to make data change in "database" you have to follow the link.

Upvotes: 1

Related Questions