How to change email in firebase auth?

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

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

But I get ... changeEmail is not a function error . I found a link here from an old fire base document.

So how do I do this in version 3.x? Because I can not find the link in the new documentation.

+18
source share
3 answers

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

Since this is a user object, your user should already be logged in. Therefore, it requires only a password.

Simple use:

 firebase.auth() .signInWithEmailAndPassword(' you@domain.com ', 'correcthorsebatterystaple') .then(function(userCredential) { userCredential.user.updateEmail(' newyou@domain.com ') }) 
+32
source

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 re-authenticate the login before calling it, because Firebase requires new authentication to perform certain account functions, such as deleting the account, changing the email address or password.

For the project in which I just implemented this, I simply included the login as part of the password / email change form, and then called "signInWithEmailAndPassword" just before calling "updateEmail".

To update your password, simply do the following:

 this.af.auth.currentUser.updatePassword(password) .then(() => { ... }); 
+1
source

You can use the following with all activities, such as verification and confirmation by email.

Variable declaration

 // Widgets private EditText etOldEmail, etNewEmail; private Button btnChangeEmail; // Firebase private FirebaseAuth mAuth; private FirebaseUser fbUser; 

Oncreate

 // Widgets etOldEmail = findViewById(R.id.a_change_email_et_old_email); etNewEmail = findViewById(R.id.a_change_email_et_new_email); btnChangeEmail = findViewById(R.id.a_change_email_btn_change_email); // Firebase mAuth = FirebaseAuth.getInstance(); fbUser = mAuth.getCurrentUser(); 

Function for changing email address

 private void changeEmailAddress() { String oldEmail = etOldEmail.getText().toString(); String newEmail = etNewEmail.getText().toString(); if (TextUtils.isEmpty(oldEmail)) { Toast.makeText(mContext, "Please Enter Your Old Email", Toast.LENGTH_SHORT).show(); } else if (TextUtils.isEmpty(newEmail)) { Toast.makeText(mContext, "Please Enter Your New Email", Toast.LENGTH_SHORT).show(); } else { String email = fbUser.getEmail(); if (!oldEmail.equals(email)) { Toast.makeText(mContext, "Wrong Current Email, Please Check Again", Toast.LENGTH_SHORT).show(); } else { fbUser.updateEmail(newEmail).addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { if (task.isSuccessful()) { fbUser.sendEmailVerification().addOnCompleteListener(new OnCompleteListener<Void>() { @Override public void onComplete(@NonNull Task<Void> task) { Toast.makeText(mContext, "Verification Email Sent To Your Email.. Please Verify and Login", Toast.LENGTH_LONG).show(); // Logout From Firebase FirebaseGeneral firebaseGeneral = new FirebaseGeneral(); firebaseGeneral.logoutUser(mContext); } }); } else { try { throw Objects.requireNonNull(task.getException()); } // Invalid Email catch (FirebaseAuthInvalidCredentialsException malformedEmail) { Toast.makeText(mContext, "Invalid Email...", Toast.LENGTH_LONG).show(); } // Email Already Exists catch (FirebaseAuthUserCollisionException existEmail) { Toast.makeText(mContext, "Email Used By Someone Else, Please Give Another Email...", Toast.LENGTH_LONG).show(); } // Any Other Exception catch (Exception e) { Toast.makeText(mContext, e.getMessage(), Toast.LENGTH_LONG).show(); } } } }); } } } 
-1
source

Source: https://habr.com/ru/post/1257830/


All Articles