63

I am trying to change/update a user's email address using :

firebase.auth().changeEmail({oldEmail, newEmail, password}, cb)

But I am getting ...changeEmail is not a function error. I found the reference here from the old firebase docu.

So how to I do it in the 3.x version? Because I cant find a reference in the new documentation.

9 Answers 9

109

You're looking for the updateEmail() method on the firebase.User object: https://firebase.google.com/docs/reference/js/firebase.User#updateEmail

Since this is on the user object, your user will already have to be signed in. Hence it only requires the password.

Simple usage:

firebase.auth()
    .signInWithEmailAndPassword('[email protected]', 'correcthorsebatterystaple')
    .then(function(userCredential) {
        userCredential.user.updateEmail('[email protected]')
    })
3
  • Great! So it was on the guides link, dang it! I was reading on the reference link the whole time. Thanks!
    – jofftiquez
    Oct 7, 2016 at 6:03
  • This is from the reference docs. I use those more than the guides for specific use-cases like this. Oct 7, 2016 at 6:13
  • 2
    I see, but I found the answer here as well :D firebase.google.com/docs/auth/web/…
    – jofftiquez
    Oct 8, 2016 at 0:29
38

If someone is looking for updating a user's email via Firebase Admin, it's documented over here and can be performed with:

admin.auth().updateUser(uid, {
  email: "[email protected]"
});
2
  • 1
    Thank you so much. This is the cleanest way to do it.
    – Shan Surat
    Feb 13, 2021 at 5:47
  • 2
    Would this fully update it across the entire auth service? I'm worried about changing someone's email using the admin SDK and this change not being fully propagated across the Authentication Service.
    – dingo
    Sep 6, 2022 at 10:14
12

FOR FIREBASE V9 (modular) USERS:

The accepted answer will not apply to you. Instead, you can do this, i.e., import { updateEmail } and use it like any other import. The following code was copy/pasted directly from the fb docs at https://firebase.google.com/docs/auth/web/manage-users

Happy coding!

import { getAuth, updateEmail } from "firebase/auth";
const auth = getAuth();
updateEmail(auth.currentUser, "[email protected]").then(() => {
  // Email updated!
  // ...
}).catch((error) => {
  // An error occurred
  // ...
});
1
  • Thank you it worked. if any others trying this code please don't forget to re-login (firebase needs recent login token) firebase user. then only firebase will allow to change email address. Feb 11, 2022 at 11:16
2

You can do this directly with AngularFire2, you just need to add "currentUser" to your path.

this.af.auth.currentUser.updateEmail(email)
.then(() => {
  ...
});

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.

For the project I just implemented this on, I just included the login as part of the change password/email forms and then called "signInWithEmailAndPassword" just prior to the "updateEmail" call.

To update the password just do the following:

this.af.auth.currentUser.updatePassword(password)
.then(() => {
  ...
});
4
  • 5
    Using angularfire2 may not be 100% relevant to the question.
    – jofftiquez
    Oct 30, 2018 at 15:39
  • It helps a lot, works with Invertase as well and explains the process, thanks
    – Simon
    Nov 19, 2020 at 1:14
  • Im getting the error The property 'auth' is not available for the type 'AngularFireAuth' - is there any hint?
    – bastifix
    May 21, 2021 at 8:25
  • Is it specified in the docs about what certain actions requires re-login? Feb 3, 2023 at 15:21
2

updateEmail needs to happen right after sign in due to email being a security sensitive info
Example 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
                }
            }
1
async updateEmail() {
const auth = firebase.auth();
try {
  const usercred = await auth.currentUser.updateEmail(this.email.value);
  console.log('Email updated!!')
} catch(err) {
  console.log(err)
}
}

You can use this to update email with Firebase.

1

Firebase v9:

const changeEmail = (userInput) => {
        const { newEmail, pass } = userInput
        signInWithEmailAndPassword(auth, oldEmail, pass)
            .then(cred => updateEmail(cred.user, newEmail))
    }
0

Before changing the email, re-login the user

Look at my code

    Future<String> updateemail(String email, String password) async {
//email:new email
    var message = "error";
    try {
      final userCredential = await FirebaseAuth.instance
          .signInWithEmailAndPassword(
              email: _auth.currentUser!.email.toString(), password: password);
      final user = userCredential.user;
      await user?.updateEmail(email).then((value) => message = "Success");
    } catch (e) {}
    return message;
  }
0

in React Native you can use this:

import auth from '@react-native-firebase/auth';

export const updateUseremail = async (email: string) => {
  return auth().currentUser?.updateEmail(email);
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.