且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

可以在Javascript中同步检查Firebase 3身份验证状态?

更新时间:2023-12-03 11:22:28

没有。使用 .onAuthStateChanged 来确定用户身份验证状态。



currentUser 这是通过修改您的HTML / CSS修复的问题。默认视图不应该显示登录元素。仅在 .onAuthStateChanged 后面显示用户未登录。


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.