Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase: Observe email verification status in real time

How would I be able to check in real time if the user verified his email?

My flow is like this:

  1. User registers
  2. Email is sent
  3. User sees "Please verify your email" notification

Now I would like to:

  1. setInterval -> check if email is verified
  2. If verified show the "Email verified" notification

For this I would need a method that fetches the user data from firebase. Usually you just use the onAuthStateChanged callback to get userdata but I need to explicitly fetch current data.

How would I do that?

like image 544
ThatBrianDude Avatar asked May 10 '18 11:05

ThatBrianDude


1 Answers

Found a way!

firebase.auth().currentUser.reload()

will fetch current user data. So all I have to do is this:

              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)
like image 155
ThatBrianDude Avatar answered Oct 21 '22 23:10

ThatBrianDude