Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Firebase cannot refresh email verification status

In my Android app, I am creating user and sending verification email. I want to proceed to the next page when the user has verified by clicking the link in the email received. However, the verification status didn't update so I cannot proceed. I have tried signing out and signing in again which works but I don't want to refresh the status in this way. Any ideas?

Here is my code:

public void onClickContinueBtn() {
   final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
   user.reload().addOnCompleteListener(new OnCompleteListener<Void>() {
       @Override
       public void onComplete(@NonNull Task<Void> task) {
           if(task.isSuccessful()) {
               if(!user.isEmailVerified()){
                   // not verified block
                   // get into here even if verified
               } else {
                   // email verified, go to next page
               }
           } else {
               // do nothing, or show error
           }
       }
   });
}
like image 743
chengsam Avatar asked Nov 29 '16 06:11

chengsam


Video Answer


2 Answers

I had same issue. I'm using FirebaseUI and automatically it creates user on Firebase DB even if email has not yet been verified. But Firebase give you possibility to check if email has been verified or not by means of method user.isEmailVerified.Two possibilities:

  1. If you want to guarantee access to your app only when email is verified: you could open a dialog when user signedIn and wait for verification. To cancel dialog automatically (only when email is verified) you can add an observer to that variable and so you can give access to your app.
  2. If you give access to your app also without verification of email, you can check it with boolean isEmailVerified. Remember that you have to refresh current user with method reload() on FirebaseUser. Note that the update of that boolean is not instantaneous
like image 161
Federico Rizzo Avatar answered Oct 09 '22 18:10

Federico Rizzo


You can refresh the user by calling reload after every few seconds. Example:

final Handler handler = new Handler();
        final int delay = 5000; //milliseconds

        handler.postDelayed(new Runnable(){
            public void run(){
                Toast.makeText(EmailVerificationActivity.this, "I'm here", Toast.LENGTH_SHORT).show();
                handler.postDelayed(this, delay);
            }
        }, delay);
like image 38
Francisca Mkina Avatar answered Oct 09 '22 18:10

Francisca Mkina