Firebase 3 - ref.getAuth () equivalent

In Firebase 2, I was able to do a synchronous check to check if the user was logged in, for example:

if (!ref.getAuth()) ..go to login page else ..show page 

I can access the currentUser state using firebase.auth().currentUser , so I tried to do:

 if (!firebase.auth().currentUser) ..go to login page else ..show page 

The problem is that it is always initially zero, so it is redirected to the login page, and then onAuthStateChanged happens after a few seconds and the user logs in.

Is there another way to check this synchronously? Or else is there a promise that I can listen to so that I can show the bootloader or something else? Thanks.

+6
source share
2 answers

I use an auth status listener to determine if a user has been signed.

  auth.onAuthStateChanged(function(user) { console.log('authStateChanged', user); if (user) { console.log("Welcome UID:" + user.uid); } }); 

When you immediately attach a listener, it will light up with the correct state.

When I launched the application, log in and reload the page, log outs:

authStateChanged Fl {...}

Welcome UID: 052289713245805425655890E9024AB4ADBA5404B3C0

So, you can use the first time the listener is called to determine if the user has already been subscribed.

+2
source

I had the same problem, and since I am using Angular, I found a synchronous call to $ getAuth () in AngularFire. https://github.com/firebase/angularfire/blob/master/docs/reference.md#getauth

There may be an equivalent in the ReactFire library. :)

0
source

All Articles