Android Firebase AuthStateListener Email Verified

Viewed 3013

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.

2 Answers

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.

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.

Related