问题
I'm having a problem with the function to update the user email. The function does three main things. First, it changes the email to the Firebase Auth, then it updates the email in the database, and last it sends the email verification.
I'm able to catch the incorrect password error, but when it comes to auth/email-already-in-use
error, it doesn't work.
The problem is that I'm updating the email in the database and sending the verification email, but it's not updating the email in Firebase Auth.
component.ts (UPDATED!!)
updateEmail(emailForm: NgForm){
let newEmail = emailForm.value.newEmail;
let password = emailForm.value.password;
this.profileProvider.updateEmail(newEmail, password)
.then(value => {
this.showEditEmail = !this.showEditEmail;
this.authService.emailVerification()
.then(value => {
this.snackBar.open('Su correo ha sido modificado con éxito. Hemos enviado un enlace de verificación a su nueva dirección de correo electrónico');
})
.catch(error => {
console.log(`Chao ${error.message}`)
})
})
.catch(error => {
if (error) {
console.log(`Hola ${error.message}`);
this.emailPasswordError = error.message;
} else {
this.emailPasswordError = null;
}
})
}
profile-provider.service.ts
updateEmail(newEmail: string, password: string): firebase.Promise<any> {
const credential = firebase.auth.EmailAuthProvider
.credential(this.currentUser.email, password);
return this.currentUser.reauthenticateWithCredential(credential)
.then( user => {
this.currentUser.updateEmail(newEmail).then( user => {
this.userProfile.update({ email: newEmail });
});
});
};
回答1:
It seems like updateEmail
is throwing it due to the new email already being used, you can catch here:
this.currentUser.updateEmail(newEmail).then(user => {
this.userProfile.update({ email: newEmail });
}, error => {console.log(error);});
来源:https://stackoverflow.com/questions/46980318/cannot-catch-auth-email-already-in-use-when-updating-email