如何在firebase auth中更改电子邮件?

时间:2016-10-07 05:21:30

标签: firebase firebase-authentication

我正在尝试使用以下方式更改/更新用户的电子邮件地址:

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

但我得到 ... changeEmail不是函数错误。我从旧的firebase文档中找到了引用here

那么如何在3.x版本中执行此操作?因为我无法在新文档中找到引用。

5 个答案:

答案 0 :(得分:25)

您正在updateEmail()对象上寻找firebase.User方法:https://firebase.google.com/docs/reference/js/firebase.User#updateEmail

由于这是在用户对象上,因此您的用户必须已登录。因此它只需要密码。

简单用法:

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

答案 1 :(得分:8)

如果某人希望通过 Firebase管理员来更新用户的电子邮件,则该文件已记录在here上,可以通过以下方式执行:

admin.auth().updateUser(uid, {
  email: "modifiedUser@example.com"
});

答案 2 :(得分:0)

您可以直接使用AngularFire2进行此操作,只需在路径中添加“ currentUser”即可。

this.af.auth.currentUser.updateEmail(email)
.then(() => {
  ...
});

您还需要在重新登录之前重新登录,因为Firebase需要全新的身份验证才能执行某些帐户功能,例如删除帐户,更改电子邮件或密码。

对于我刚刚实施的项目,我只是将登录名包含在更改密码/电子邮件表单中,然后在“ updateEmail”调用之前将其称为“ signInWithEmailAndPassword”。

要更新密码,只需执行以下操作:

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

答案 3 :(得分:0)

您可以将以下内容与验证和电子邮件验证等完整功能结合使用。

变量声明

// 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();

更改电子邮件地址的功能

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();

                        }
                    }

                }
            });

        }

    }

}

答案 4 :(得分:0)

由于电子邮件是安全敏感的信息,

updateEmail需要在登录后立即发生
Kotlin示例

 // need to sign user in immediately before updating the email 
        auth.signInWithEmailAndPassword("currentEmail","currentPassword")
        .addOnCompleteListener(this) { task ->
                if (task.isSuccessful) {
                    // Sign in success now update email                
                    auth.currentUser!!.updateEmail(newEmail)
                        .addOnCompleteListener{ task ->
                        if (task.isSuccessful) {
               // email update completed
           }else{
               // email update failed
                    }
       }
       } else {
                    // sign in failed
                }
            }