首页 > 解决方案 > Firebase:实时观察电子邮件验证状态

问题描述

如果用户验证了他的电子邮件,我将如何实时检查?

我的流程是这样的:

  1. 用户注册
  2. 电子邮件已发送
  3. 用户看到“请验证您的电子邮件”通知

现在我想:

  1. setInterval -> 检查电子邮件是否经过验证
  2. 如果验证显示“电子邮件验证”通知

为此,我需要一种从 firebase 获取用户数据的方法。通常您只需使用 onAuthStateChanged 回调来获取用户数据,但我需要显式获取当前数据。

我该怎么做?

标签: firebasefirebase-authentication

解决方案


找到方法了!

firebase.auth().currentUser.reload()

将获取当前用户数据。所以我所要做的就是:

              this.checkForVerifiedInterval = setInterval(() => {
                firebase.auth()
                  .currentUser
                  .reload()
                  .then(ok => {
                    if (firebase.auth().currentUser.emailVerified) {
                      this.props.history.push("/verification-email-verified")
                      window.Materialize.toast("Email verified.", 3000)
                      clearInterval(this.checkForVerifiedInterval)
                    }
                  })
              }, 1000)

推荐阅读