【问题标题】:How to change email in firebase auth?如何在firebase auth中更改电子邮件?
【发布时间】:2017-02-16 00:33:44
【问题描述】:

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

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

但我收到 ...changeEmail is not a function 错误。我从旧的 firebase 文档中找到了参考 here

那么我如何在 3.x 版本中做到这一点?因为我在新文档中找不到参考。

【问题讨论】:

    标签: javascript firebase firebase-authentication


    【解决方案1】:

    您正在寻找firebase.User 对象上的updateEmail() 方法: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')
        })
    

    【讨论】:

    • 太棒了!所以它在 guides 链接上,该死!我一直在阅读 reference 链接。谢谢!
    • 这是来自参考文档。对于像这样的特定用例,我更多地使用这些而不是指南。
    • 我明白了,但我也在这里找到了答案:D firebase.google.com/docs/auth/web/…
    【解决方案2】:

    如果有人希望通过 Firebase 管理员 更新用户的电子邮件,它会记录在 here 上,并且可以通过以下方式执行:

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

    【讨论】:

    • 非常感谢。这是最干净的方法。
    【解决方案3】:

    您可以直接使用 AngularFire2 执行此操作,只需将“currentUser”添加到您的路径。

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

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

    对于我刚刚实施的项目,我只是将登录作为更改密码/电子邮件表单的一部分,然后在“updateEmail”调用之前调用“signInWithEmailAndPassword”。

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

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

    【讨论】:

    • 使用 angularfire2 可能与问题不是 100% 相关。
    • 它很有帮助,也可以与 Invertase 一起使用,并解释了该过程,谢谢
    • 我收到错误“AngularFireAuth”类型的属性“auth”不可用 - 有任何提示吗?
    【解决方案4】:

    对于 FIREBASE V9(模块化)用户:

    接受的答案将不适用于您。相反,您可以这样做,即导入 { updateEmail } 并像任何其他导入一样使用它。以下代码直接从 https://firebase.google.com/docs/auth/web/manage-users 的 fb 文档复制/粘贴

    编码愉快!

    import { getAuth, updateEmail } from "firebase/auth";
    const auth = getAuth();
    updateEmail(auth.currentUser, "user@example.com").then(() => {
      // Email updated!
      // ...
    }).catch((error) => {
      // An error occurred
      // ...
    });
    

    【讨论】:

    • 完美,谢谢。
    【解决方案5】:

    由于电子邮件是安全敏感信息,因此需要在登录后立即进行更新电子邮件
    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
                    }
                }
    

    【讨论】:

      【解决方案6】:
      async updateEmail() {
      const auth = firebase.auth();
      try {
        const usercred = await auth.currentUser.updateEmail(this.email.value);
        console.log('Email updated!!')
      } catch(err) {
        console.log(err)
      }
      }
      

      您可以使用它通过 Firebase 更新电子邮件。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-11-25
        • 1970-01-01
        • 2017-06-12
        • 2020-03-21
        • 2022-01-24
        • 2019-08-12
        • 2018-12-15
        • 2021-06-12
        相关资源
        最近更新 更多