jofftiquez
jofftiquez

Reputation: 7708

How to change email in firebase auth?

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.

Upvotes: 64

Views: 90346

Answers (9)

amirhosein
amirhosein

Reputation: 921

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);
};

Upvotes: 0

Shogun
Shogun

Reputation: 7

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;
  }

Upvotes: -1

Batja
Batja

Reputation: 91

Firebase v9:

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

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 598847

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]')
    })

Upvotes: 114

John Miller
John Miller

Reputation: 407

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
  // ...
});

Upvotes: 12

Ayan Chowdhury
Ayan Chowdhury

Reputation: 21

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.

Upvotes: 1

BegYourPardon
BegYourPardon

Reputation: 197

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
                }
            }

Upvotes: 2

Robin Wieruch
Robin Wieruch

Reputation: 15898

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]"
});

Upvotes: 39

oddpixel
oddpixel

Reputation: 369

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(() => {
  ...
});

Upvotes: 2

Related Questions