1

I want the program to repeatedly check if the user is verified in the Firebase Authentification until it is. For example, when the user makes an account on my application, the program switches into a new activity, waits until the user is verified, then automatically continues into the next activity.

I attempted to do this using a do while loop, but unfortunately, the program freezes and stays at a black screen. I never encountered any error messages along the way.

I tried the program with these lines of code:

EmailVerified = false;

do {
    if (mAuth.getCurrentUser().isEmailVerified()) {
        EmailVerified = true;
        Intent intent = new Intent(VerificationEmailPage.this, MainActivity.class);
        startActivity(intent);
    }
} while (!EmailVerified);

Doing this caused the activity not to load, and simply turn black. It seemed as if no other code was also executed in the activity. I needed the activity to load and constantly keep checking if the user is verified, then switch to the next activity once it was.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
MJV
  • 652
  • 5
  • 14
  • 1
    this is basically an infinite loop. As @Doug mentioned, check it when the user is signed in.. like user.isEmailVerified() – Masoom Badi Dec 31 '19 at 22:25

2 Answers2

3

What you're doing now is not a good idea at all. Instead, you should be using an AuthStateListener to find out when the user is signed in.

mAuthListener = new FirebaseAuth.AuthStateListener() {
    @Override
    public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
        FirebaseUser user = firebaseAuth.getCurrentUser();
        if (user != null) {
            // Sign in logic here.
        }
    }
};

// activate it:
FirebaseAuth.getInstance().addAuthStateListener(mAuthListener);

// deactivate it:
FirebaseAuth.getInstance().removeAuthStateListener(mAuthListener);


The listener will get invoked any time the user is signed in or out, until the listener is removed.

Read more about how it works: How does the firebase AuthStateListener work?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
  • What is ```mAuthListener``` at the beginning of the listener? It seems like a variable to me. – MJV Dec 31 '19 at 22:35
  • You feed that to addAuthStateListener() to activate it, then again to removeAuthStateListener() to stop it. – Doug Stevenson Dec 31 '19 at 23:02
  • I'm sorry if I don't understand, I'm pretty new to Firebase in general, what I understand is that this listener will check in the user is signed in, and it will execute the code inside. What I want to do is to constantly keep checking if the user is verified after the user signs up. – MJV Dec 31 '19 at 23:32
  • The listener constantly reacts to the state of the user. You shouldn't write a loop for that. It obviously has a bad effect on your page. – Doug Stevenson Jan 01 '20 at 00:13
  • I discovered something else interesting while checking through this question: https://stackoverflow.com/questions/11295152/how-to-run-an-infinite-loop-in-android-without-freezing-the-ui, using `AsyncTask`, a `while` loop works fine in the activity without any problems, but when I ran `if (mAuthUser.isEmailVerified()` in the loop, even though my Email was verified, it didn't run the code inside the `if` statement. I guess there might be a new issue here. My user was also signed in at the moment. – MJV Jan 01 '20 at 01:38
  • 1
    I still wouldn't recommend that, as polling in a loop is a waste of CPU and battery. Use the listener for a more efficient approach that's easier to implement. – Doug Stevenson Jan 01 '20 at 02:16
  • I'll attempt using @samthecodingman's method, while still using an `AuthStateListener` – MJV Jan 01 '20 at 16:05
0

Building upon Doug's answer, this is the best method for doing what you want:

  1. Log in user (whether by sign in or creating an account).
  2. Start a "loading" activity, this activity gets the current user using a FirebaseAuth.AuthStateListener.
  3. In the onAuthStateChanged handler, check firebaseAuth.getCurrentUser().isEmailVerified().
  4. If the user is verified, send them to the next activity.
  5. If not, show a UI that asks the user to send a verification email.
  6. When the user presses the send button, send a verification email configured to point back to your application (see Passing State in Email Actions).
  7. When the user hits the URL from the verification email, check their isEmailVerified() state again.
  8. Jump back to step 4.
samthecodingman
  • 23,122
  • 4
  • 30
  • 54