简体   繁体   中英

Possible to check Firebase 3 auth state synchronously in Javascript?

Currently my application is making use of the authorization state change callbacks to indicate moving between the authorization and the main page sections in my app. (Note: I am not using AngularFire)

In Firebase 3 there is firebase.auth().currentUser however the currentUser is set to null if firebase hasn't finished pulling the data from the server. This value may be null at startup but be set 2 seconds later without the user doing anything with the application.

In my app, the users authorization state should be the determining factor of the first screen that they see, because this is asynchrnous in nature, they're seeing the Authorization page, then after the currentUser updates it's moving them to the home page, which displays a very tacky page switch.

Splash Screen -> Login -> Home

The amount of time on the Login page depends on the internet connection, however on mobile data it is extremely noticable.

Is there any way to force a check to see if the user is logged in that will return true or false? I can make an asynchronous method into a promise, the issue is that the onAuthStateChanged callback isn't called on startup if the user is logged out already.

No. Use .onAuthStateChanged to determine the users Auth state.

In terms of the login showing / blinking prior to currentUser that is an issue fixed by modifying your HTML/CSS. The default view should not display the login elements. Only display that after .onAuthStateChanged tells you the user is not logged in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM