Beth Mezias
Beth Mezias

Reputation: 270

Firebase user changes email or phone number

Building an Android app with a Firebase backend the next step in development is to configure the API for buying and selling goods inside the app. The API will use the phone number and naturally, it will keep an audit trail of transactions. Firebase SMS authentication is a good match for the app.

However, this app is not intended for the US. Users may want to change carriers and that could mean changing phone numbers. What do we do if the user changes their phone number? A similar issue could come up if the model were email and the user were changing their email account (e.g. changing the app's email login account from gmail.com to facebook.com).

It looks like this would be a manual process where the user can work with our admin team to create the new account, validate it with an SMS and then find and replace the old UUID with the new UUID. When the refs are updated with the new user key, all the data will move into the new account.

Is there a better way? Has anybody got a 'user move' script that might be adapted to our app?

Any insight from the Firebase team is appreciated.

Upvotes: 1

Views: 2207

Answers (2)

Vedprakash Wagh
Vedprakash Wagh

Reputation: 3712

You have to re-authenticate the current user using

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
AuthCredential credential = EmailAuthProvider.getCredential("[email protected]", "userPassword");
user.reauthenticate(credential).addOnCompleteListener(
    //Code to handle successful completion of authentication.
);

You did the above re-authentication to check whether correct user is trying to change the email address.

After doing the above authentication, if the onComplete in addOnCompleteListener is successful, then you have to update the current email using the method below.

user.updateEmail("[email protected]").addOnCompleteListener();

Again, check whether the updating email was successful in onComplete(){} and take the appropriate actions.

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

Firebase Authentication has methods to change the phone number and email address on an existing account. These don't change the UID of the user, so won't require you to update the database.

Upvotes: 2

Related Questions