Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 940
kartik Avatar asked Mar 19 '18 06:03

kartik


People also ask

How do I change my Firebase email?

Open your project in the Firebase console. Go to the Email Templates page in the Auth section. In any of the Email Types entries, click the pencil icon to edit the email template. Click customize action URL, and specify the URL to your custom email action handler.

Can you change email in Firebase Auth?

You will also need to reauthenticate the login prior to calling this as Firebase requires a fresh authentication to perform certain account functions such as deleting the account, changing the email or the password.


1 Answers

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.");
                                        }
                                    }
                                });
                        //----------------------------------------------------------\\
                    }
                });
like image 72
Rahul Chandrabhan Avatar answered Sep 29 '22 07:09

Rahul Chandrabhan