Firebase 9 (modular sdk) replacement for currentUser.updateEmail

Viewed 473

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

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, "new@email.tld").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.

Related