6

I have a SignInActivity with Firebase AuthStateListener.

final FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
final FirebaseAuth.AuthStateListener firebaseAuthListener = new FirebaseAuth.AuthStateListener() {
    @Override
    public void onAuthStateChanged(FirebaseAuth auth) {
        FirebaseUser user = auth.getCurrentUser();
        if (user != null && user.isEmailVerified()) {
            firebaseAuth.removeAuthStateListener(this);
            startActivity(new Intent(LoginActivity.this, MainActivity.class));
        }
    }
};
firebaseAuth.addAuthStateListener(firebaseAuthListener);

When I successfully registered a new Account, I setVisibity(View.Visible) a verify page with EditTextEmail & VerifyButton inside the activity (in case someone wants to resend the email verification).

What I want to do is when I verify my email from my email account, I want the page to automatically start my MainActivity instead of just staying idle in my LoginActivity, like SMS verification, when verification code received in SMS, the app reads the SMS and navigate to MainActivity. Is it possible to achieve this with email verification? Because the FirebaseAuthState never changed even after I click on verification link on my email.

I need something like OnFirebaseAuthUserEmailVerifiedListener

I'm new to firebase, please kindly give me advice on how to achieve this or if it is not possible.

  • 1
    I now it's been 10 months since you started this but I will share my idea for future readers. I believe that you can add some code to onStart() lifecycle method. This code will check if the user has verified his email and if he has then you can do whatever you want. I believe this is good because if the user wants to verify his email he has to put your app in the background. By the way I am facing the same problem so if you have find any elegant solution please share it here! – Kwnstantinos Nikoloutsos Aug 18 '18 at 00:31

2 Answers2

5

This link is really useful.

Because the FirebaseAuthState never changed even after I click on verification link on my email.

That's because the user is cached, and you need to reload the user:

Do note that the FirebaseUser object is cached within an app session, so if you want to check on the verification state of a user, it's a good idea to call .getCurrentUser().reload() for an update.

Century
  • 285
  • 6
  • 20
  • where should I call my the reload() method? – Fadel Trivandi Dipantara Oct 05 '17 at 07:46
  • 1
    If you don't allow anything to be done until the email is verified you can spin-loop, i.e. `while(!user.isEmailVerified()){ mAuth.getCurrentUser().reload(); }`, If you can navigate through your app even with unverified email, you can add for example a Snackbar with a refresh button that call the `reload()` method – Century Oct 05 '17 at 07:49
  • Thank you for your advice. I will add Snackbar to refresh the user auth. – Fadel Trivandi Dipantara Oct 05 '17 at 07:55
0

You need something like this

     FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();

    if (user.isEmailVerified())
    {
        // user is verified, so you can finish this activity or send user to activity which you want.
        finish();
        Toast.makeText(LoginActivity.this, "Successfully logged in", Toast.LENGTH_SHORT).show();
    }
    else
    {
        // email is not verified, so just prompt the message to the user and restart this activity.


        sendVerificationEmail();

    }
}

And method to get emailVerification

private void sendVerificationEmail(){

    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    user.sendEmailVerification()
            .addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if (task.isSuccessful()) {
                        // email sent
            // after email is sent just logout the user and finish this activity
                        FirebaseAuth.getInstance().signOut();
                        startActivity(new Intent(SignupActivity.this, LoginActivity.class));
                        finish();
                    }
                    else{
                        // email not sent, so display message and restart the activity or do whatever you wish to do

                    }
                }
    });
}

Hope this helps you.

pankaj sharma
  • 181
  • 2
  • 12
Abdul Kawee
  • 2,687
  • 1
  • 14
  • 26