Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Firebase AuthStateListener Email Verified

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.

like image 853
Fadel Trivandi Dipantara Avatar asked Oct 05 '17 07:10

Fadel Trivandi Dipantara


Video Answer


1 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.

like image 97
Century Avatar answered Oct 17 '22 16:10

Century