首页 > 解决方案 > 如何知道电子邮件是否已经在firebase身份验证中注册,javascript?

问题描述

我正在为用户进行电子邮件更新,但首先我需要知道该电子邮件之前是否已在 Firebase 身份验证中注册。

通过这种方式,我更新了电子邮件:

 if(administrador.correo != vm.editedItem.correo){
       console.log("ESTA ACTUALIZANDO CORREO");
       console.log(vm.editedItem);
       firebase.auth().signInWithEmailAndPassword(vm.editedItem.correo, vm.editedItem.contrasenia)
                                    .then(function(userCredential) {
                                        console.log("USER CREDENTIAL");
                                        console.log(userCredential);
                                        userCredential.user.updateEmail(vm.editedItem.correo)
                                        .then(function() {
                                            console.log("email update");
                                        // Update successful.
                                        }).catch(function(error) {
                                            console.log("ERROR");
                                            console.log(error);
                                        // An error happened.
                                        });
                                    })
                              }

但在我可以更新电子邮件之前,我必须验证它是否已经存在,如果电子邮件存在,因为它不允许更新邮件,如果电子邮件不存在,因此我更新电子邮件。

预先非常感谢您。

标签: javascriptfirebasefirebase-authentication

解决方案


检查下面的代码。我认为您正在尝试更改您登录时使用的同一电子邮件。我做了一些小的更改和评论。

if (administrador.correo != vm.editedItem.correo) {
    console.log("ESTA ACTUALIZANDO CORREO");
    console.log(vm.editedItem);
    firebase.auth().signInWithEmailAndPassword(vm.currentItem.correo, vm.editedItem.contrasenia)  // sign in with current email and password
        .then(function (userCredential) {
            console.log("USER CREDENTIAL");
            console.log(userCredential);
            userCredential.user.updateEmail(vm.editedItem.correo)  // update new email
                .then(function () {
                    console.log("email update");
                    // Update successful.
                }).catch(function (error) {
                    console.log("ERROR");
                    console.log(error);
                    // An error happened.
                    // if updated user email already exists, it returns error code: auth/email-already-in-use
                });
        })
}

点击此链接获取更多更新电子邮件


推荐阅读