elranu
elranu

Reputation: 2312

Get if a user is authenticated on Firebase initializeApp

When I do:

firebase.initializeApp(config);
firebase.auth().currentUser;

the first time after a refresh for some milliseconds currentUser is null. But later the currentUser value gets filled. Of course previously I authenticated without sign out.

How can I initializeApp and wait till I really know the user is loggedin or not? I mean firebase.initializeApp and firebase.auth() are not promises. Is there any promise to know that the user is logged in or not? I could not find any.

Upvotes: 2

Views: 1624

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598817

I now often use an onAuthStateChanged() handler to detect if the user is signed in.

  firebase.initializeApp(config);
  firebase.auth().onAuthStateChanged((user) => {
      if (user == null) {
          // TODO: start sign-in flow
      }
      else {
          // TODO: start actual work
      }
    }
  });

Upvotes: 3

Related Questions