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

Gra*_*ton 4 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.
});
Run Code Online (Sandbox Code Playgroud)

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

var user = firebase.auth().currentUser;

user.sendEmailVerification().then(function() {
  // Email sent.
}).catch(function(error) {
  // An error happened.
});
Run Code Online (Sandbox Code Playgroud)

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

boj*_*eil 6

是的,您只有在验证后才能更改电子邮件。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.
  });
Run Code Online (Sandbox Code Playgroud)