kartik
kartik

Reputation: 139

How to update email from Firebase in Android?

I am using this code:

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

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

But still I am not able to update user Email ID for logged in person. Other things working fine but not this.

Upvotes: 10

Views: 14352

Answers (2)

BegYourPardon
BegYourPardon

Reputation: 197

For Kotlin

 // need to sign user in immediately before updating the email 
        auth.signInWithEmailAndPassword("currentEmail","currentPassword")
        .addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    // Sign in success now update email                
                    auth.currentUser!!.updateEmail(newEmail)
                        .addOnCompleteListener{ task ->
                        if (task.isSuccessful) {
               // email update completed
           }else{
               // email update failed
                    }
       }
       } else {
                    // sign in failed
                }
            }

Upvotes: 1

Rahul Singh Chandrabhan
Rahul Singh Chandrabhan

Reputation: 2496

You need to re-authenticate your user. As according to documentation changing primary email address is a sensitive action.

Re-Authentication :

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        // Get auth credentials from the user for re-authentication
        AuthCredential credential = EmailAuthProvider
                .getCredential("[email protected]", "password1234"); // Current Login Credentials \\
        // 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) {
                        Log.d(TAG, "User re-authenticated.");
                        //Now change your email address \\
                        //----------------Code for Changing Email Address----------\\
                        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
                        user.updateEmail("[email protected]")
                                .addOnCompleteListener(new OnCompleteListener<Void>() {
                                    @Override
                                    public void onComplete(@NonNull Task<Void> task) {
                                        if (task.isSuccessful()) {
                                            Log.d(TAG, "User email address updated.");
                                        }
                                    }
                                });
                        //----------------------------------------------------------\\
                    }
                });

Upvotes: 21

Related Questions