【问题标题】:How to verify an email before making it the primary (Firebase Authentication)如何在将电子邮件设为主要电子邮件之前对其进行验证(Firebase 身份验证)
【发布时间】:2020-05-01 02:19:55
【问题描述】:

在 firebase auth 中,只有在我将其作为登录的主要电子邮件后,我才能验证用户的电子邮件。 我可以通过这种方式更改用户的电子邮件:

var user = firebase.auth().currentUser;

user.updateEmail("user@example.com").then(function() {
  // Update successful.
}).catch(function(error) {
  // An error happened.
});

然后我可以在这样设置后验证电子邮件:

var user = firebase.auth().currentUser;

user.sendEmailVerification().then(function() {
  // Email sent.
}).catch(function(error) {
  // An error happened.
});

我要做的是在将电子邮件设置为用户主电子邮件之前验证电子邮件。

【问题讨论】:

    标签: javascript firebase firebase-authentication


    【解决方案1】:

    是的,您只有在验证后才能更改电子邮件。 API 没有很好的文档记录。您可以通过verifyBeforeUpdateEmail 进行操作。

    firebase.auth().currentUser.verifyBeforeUpdateEmail('newEmail@example.com')
      .then(function() {
        // Verification email sent.
        /  When the user clicks the email link,
        // it will update to newEmail@example.com and set it as verified,
        // emailVerified: true.
        // Until then, the old email remains on the account.
      })
      .catch(function(error) {
        // Error occurred. Inspect error.code.
      });
    

    【讨论】:

    • 这看起来是一个很棒的解决方案,但我每次都收到这个错误:TypeError: firebase.auth(...).currentUser.verifyBeforeUpdateEmail is not a function跨度>
    • 而且我相信我导入正确,因为我能够获取 currentUser,它似乎没有功能 verifyBeforeUpdateEmail
    • 您使用的是最新版本的 SDK 吗?这是最近添加的。
    • 是的,就是这样。更新到 Firebase 7.14.2。很棒的解决方案,这比我希望的还要好。
    • @bojeil 用于生成电子邮件验证链接的文档 (firebase.google.com/docs/auth/admin/email-action-links) 声明:“使用的电子邮件必须属于现有用户。”这似乎意味着我们不能将生成的电子邮件验证链接与更新前的验证结合起来。对吗?
    猜你喜欢
    • 2021-11-21
    • 2023-03-18
    • 2018-02-10
    • 2018-09-17
    • 2021-03-26
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 2019-05-20
    相关资源
    最近更新 更多