Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase 9 (modular sdk) replacement for currentUser.updateEmail

How do we do this now:

const auth = getAuth(firebaseApp);


export async function updateUserEmail(email) {
    try {
        // let updatedUser = if need access
        await auth.currentUser.updateEmail(email);
    } catch (e) {
        alert(e.message);
        throw new Error();
    }
}

updateEmail is no longer a method

like image 275
Nikos Avatar asked Jul 05 '26 15:07

Nikos


1 Answers

You need to import updateEmail from the SDK this way now:

import firebase from "firebase/compat/app";
import { getAuth, onAuthStateChanged, updateEmail } from "firebase/auth";

// Initialize Firebase App
const app = firebase.initializeApp(firebaseConfig);
const auth = getAuth(app);

onAuthStateChanged(auth, (user) => {
  console.log("Old Email", user.email);
  updateEmail(user, "[email protected]").then(() => {
    console.log("email updated");
  }).catch((e) => {
    console.log(e);
  });
});

Also you need to pass the user object itself in the updateEmail function so for testing purpose I've added the code in onAuthStateChanged but you can fetch the object or actually store it when page loads.

like image 74
Dharmaraj Avatar answered Jul 07 '26 03:07

Dharmaraj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!