Daniel Vargas Avila
Daniel Vargas Avila

Reputation: 11

Change email to an user in firebase

Im using firebase for my react native app. I want to add a feature so users can change their email, My code is something like:

// Function to update user's email
  updateEmail(user, newEmail)
    .then(() => {
      console.log("Email updated successfully");
    })
    .catch((error) => {
      console.error("Error updating email:", error);
    });
  }
};

But I get the error:

ERROR Error updating email: [FirebaseError: Firebase: Please verify the new email before changing email. (auth/operation-not-allowed).]

But I'm not able to send an email verification to an email different of that of the user.

I have tried to send the verification email, but it doesn't arrive. I read in a medium blog that the verification email is supposed to get sent automatically, but when I try that code, I get the same error.

Edit: I'm reauthenticating the user before tryng to change the email:

const reauthenticate = (password) => {
  const user = authentication.currentUser;
  const credential = EmailAuthProvider.credential(user.email, password);

  return reauthenticateWithCredential(user, credential)
    .then(() => {
      console.log("Usuario reautenticado.");
      return true;
    })
    .catch((error) => {
      console.error("Error reautenticando al usuario:", error);
      alert("Error: La contraseña es incorrecta.");
      return false;
    });
};

Upvotes: 0

Views: 38

Answers (1)

Han
Han

Reputation: 1

I think you have to reauthenticate your user and then change the email and then send a verification mail

Upvotes: 0

Related Questions