如何在将电子邮件设置为主邮件之前进行验证(Firebase身份验证)

时间:2020-05-01 02:19:55

标签: javascript firebase firebase-authentication

在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.
});

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

1 个答案:

答案 0 :(得分:4)

是的,只有在验证电子邮件之后,您才能更改电子邮件。该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.
  });