Update email and re-authenticate user – Firebase – Android


In this post, I am going to show you, how to update registered email in firebase. I have already integrated firebase in my project and created the Register and Login activity. If you haven’t done it, then I would suggest you create the register and login activity.

If you don’t know how to, then see my first two posts on firebase registration and login using email id and password. This is required because in this post we will see how to update the registered email id. And the login is required because we won’t be able to change the email id without signing in.

So in this post, we will make a welcome activity. It will open after successful login.
It will have an EditText and a Button. By default, the value of the EditText will be our registered email id and to update the id we will enter a new email id in the EditText.

firebase_email_update_dialogClicking the Button will open a dialog. A dialog to re-authenticate user.
We will create a custom dialog. In this post, I am not going to focus on creating the custom dialog. If you don’t know how to create a custom dialog, you can see our tutorial on how to create a custom dialog in android.
The dialog will have two EditTexts for the email id and password and one button for Cancel and an Ok button.

Let’s create the activity and name it WelcomeActivity and its layout as activity_welcome. Below is the code of our WelcomeActivity.java file and activity_welcome.xml file.

Do the necessary validations, like if the email is valid or not, password etc.

In the same WelcomeActivity, create a method for the dialog. Name it showDialog().

I am going to copy and paste the same showDialog code from our Custom dialog tutorial.

This is the layout of our dialog, again same dialog layout of our custom dialog tutorial.

Now clicking the Cancel button, the dialog will be canceled and clicking the Update button the email will be updated. Here in the dialog, the user will enter his/her current email id and password. Then it will check if the current email id and password match with the current user’s credentials. After successfully re-authenticating the email will be updated.

To re-authenticate, we can use the reauthenticate() method of the FirebaseUser class. Get the current user and call reauthenticate on it.

If the re-authentication is successful, we can update the email using the updateEmail() method of the FirebaseUser class.

Below is the final code of our showDialog() method

Update the onCreate method of WelcomeActivity with the below code

That’s all. Run your project. Login with your registered email id and password. Type the new email in the EditText filed of the WelcomeActivity. Click Update. The dialog box will open. Type your current Email id and password. If the authentication is successful your old email will be updated with the new one.

To confirm, open your firebase console, authentication and then users.

You still have any doubt or query, please let us know in the comment section.


You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *