2

I'm trying to implement email verification with Firebase. I've created Dynamic Links that redirect to my app successfully. I have tested the link on the web too. It works perfectly and it verifies the email. However, the link on the verification email redirects me to my app, Auth.auth().currentUser.isEmailVerified still gives me false, even though I ran Auth.auth().currentUser?.reload() command beforehand.

Any help on this?

  • Calling `reload` on the corresponding user after the action code is applied should update emailVerified to true on the user. If this doesn't happen, you need to file a bug report with Firebase Support. – bojeil Nov 07 '17 at 09:17
  • @bojeil, what do you mean with the "action code"? – Cenk Yurtbilir Nov 08 '17 at 17:04
  • It's the code that is sent along the link. It is appended to the link via `oobCode=actionCode`. You parse that code and pass it to `auth.applyActionCode` to redeem it. – bojeil Nov 09 '17 at 08:06

3 Answers3

0

You can set up a timer like this:

var verificationTimer : Timer = Timer()    // Timer's  Global declaration

self.verificationTimer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(LoginViewController.checkIfTheEmailIsVerified) , userInfo: nil, repeats: true)

and then repeatedly check user's current state using this function:

func checkIfTheEmailIsVerified(){

    FIRAuth.auth()?.currentUser?.reload(completion: { (err) in
        if err == nil{

            if FIRAuth.auth()!.currentUser!.isEmailVerified{

                let feedVCScene = self.navigationController?.storyboard?.instantiateViewController(withIdentifier: "ViewControllerVC_ID") as! ViewController
                self.verificationTimer.invalidate()     //Kill the timer
                self.navigationController?.pushViewController(feedVCScene, animated: true)
                // Segueing to the next view(i prefer the instantiation method).
            } else {

                print("It aint verified yet")

            }
        } else {

            print(err?.localizedDescription)

        }
    })

}

Hope this helps :)

Source: https://stackoverflow.com/a/40037547/6596799

Anushk
  • 482
  • 4
  • 20
  • [Anushk](https://stackoverflow.com/users/6596799/anushk) surprisingly it worked. But can you explain why it works with repeating timer but not working with just one execution of reloading of the user? – Cenk Yurtbilir Nov 05 '17 at 22:57
  • now it is not working. I'm not sure what is going on – Cenk Yurtbilir Nov 05 '17 at 23:22
  • You want to constantly check if the e-mail is verified or not. That's rather weird, can you try breakpoints at the code to check verification, to see if it's being called or not? – Anushk Nov 06 '17 at 15:40
  • It is calling the function to check if the email is verified. However, I made it iterate 1 minute which is 60 iterations, still did not work. – Cenk Yurtbilir Nov 06 '17 at 19:05
0

.reload() is async, so you can try and check isEmailVerified using .then.

Auth.auth().currentUser?.reload()
  .then(() => console.log(Auth.auth().currentUser.isEmailVerified)
0

In Kotlin AndroidStudio I used a button "Verify Email!" enter image description here

in Class:

private var b = false //verificacion email

button

btnVerify.setOnClickListener{
            verifyEmail()
        }

then

private fun verifyEmail(){
        val mAuth = FirebaseAuth.getInstance()
        mAuth.currentUser?.reload()?.addOnCompleteListener {
            b = mAuth.currentUser!!.isEmailVerified
            Log.d("VERI","state: $b")
            if(b){
                btnVerify.visibility = View.GONE
                yuorButton.visibility = View.VISIBLE //optional
            }else{
                Toast.makeText(applicationContext,"your email not verified", Toast.LENGTH_SHORT).show()
            }
        }
}

b is the email verification status. It will only be "true" if the email is verified. Attention! "reload ()" is asynchronous and "b" will take a tiny while to get "true". For me it is less than half a second.