Request for a synchronous firebase.auth().currentUser like API

1,959 views
Skip to first unread message

Dobrin Ivanov

unread,
Jun 15, 2016, 3:24:19 PM6/15/16
to Firebase Google Group
Hi all!
This is my first post! I like Firebase very much and still only experimenting :)


So Firebase 2.* has https://www.firebase.com/docs/web/api/firebase/getauth.html :
_Synchronously_ retrieves the current authentication state of the client.

And now this changed in Firebase 3 - we have : firebase.auth().currentUser might also be null because the auth object has not finished initializing

This is causing me some trouble porting FB2 code to FB3 and even for new code I'm forced to do strange things due to the async nature of the new API. For example when the user refresh the SPA app we need to use the

recommended way :
firebase.auth().onAuthStateChanged(function(user) {...});
I'm not sure what is the reason for this change. Just note that in my case most of the time I just need the uid property only and I prefer to get it synchronously - it will simplify a lot of things. I know that the session state is kept in the localStorage which can be read synchronously.

So I just want to ask the Firebase team : Please, if possible, provide a way for retrieving at least the current user ID (and probably all the other properties that are available into the localStorage) synchronously.

Thank you very much and keep up the good work!
Dobrin





Bassam Ojeil

unread,
Jun 15, 2016, 5:06:54 PM6/15/16
to Firebase Google Group
Hey Dobrin,
When you load the page, a current user also needs to be refreshed and in some cases tokens need to be refreshed to apply the latest changes to the current state (user could be deleted, properties modified externally, etc.) and both of these operations are asynchronous. In addition signInWithRedirect operations also require some async operations before updating the current state of the user. In addition some js platforms (react native) use async storage instead of the sync window.localStorage. There are numerous reasons why auth.currentUser cannot be determined synchronously.

Best regards,
Bassam

Kato Richardson

unread,
Jun 16, 2016, 3:03:16 PM6/16/16
to Firebase Google Group
Hi Dobrin,

The behavior here hasn't changed much. Version 3 has getCurrentUser(), which is synchronous and similar in behavior to getAuth() in 2.x. Both can return null if you invoke them before auth state is fetched from the server.

☼, Kato

--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To post to this group, send email to fireba...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/2dda47aa-e6a7-4250-aa99-f4eeb9318864%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--

Kato Richardson | Developer Programs Eng | kato...@google.com | 775-235-8398

Dobrin

unread,
Jun 17, 2016, 4:38:16 PM6/17/16
to Firebase Google Group
Ok, but the userId cannot change. Currently my top navigation bar is different for logged-in or anonimous users and I do not like to first show the anon version and then the other one. For now my workaround is to persist the logged-in user ID into localStorage.

thank,
Dobrin
Reply all
Reply to author
Forward
0 new messages