Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the firebase AuthStateListener work?

I am developing an android app using firebase for user management and authentication. I was wondering when the auth state listener gets called and how it works, as in my app I have a bug related to this.

Here is an example of one in my android app:

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

How does the AuthStateListener work and when does it get called?

like image 409
Tom Finet Avatar asked Jul 06 '16 15:07

Tom Finet


People also ask

How does Firebase authentication work?

Firebase Authentication provides backend services, easy-to-use SDKs, and ready-made UI libraries to authenticate users to your app. It supports authentication using passwords, phone numbers, popular federated identity providers like Google, Facebook and Twitter, and more.

Does Firebase UID change?

As said by @user663031, the answer "no" is correct.

What does Firebase auth () currentUser return?

console. log(firebase. auth(). currentUser) // This returns null console.


1 Answers

As the Firebase API says:

AuthStateListener is called when there is a change in the authentication state.

OnAuthStateChanged gets invoked in the UI thread on changes in the authentication state:

  • Right after the listener has been registered
  • When a user is signed in
  • When the current user is signed out
  • When the current user changes
  • When there is a change in the current user's token (Notice this has been removed and moved to a separate listener, see FirebaseAuth.IdTokenListener)
like image 171
Ami Hollander Avatar answered Oct 11 '22 12:10

Ami Hollander