简体   繁体   English

Firebase 9.4.1 和 Javscript 身份验证功能“onAuthStateChanged”运行缓慢

[英]Firebase 9.4.1 and Javscript Authentication function "onAuthStateChanged" works slowly

I am learning Firebase and I am using the latest version 9.4.1 for Web with Javascript and Jquery.我正在学习 Firebase,我正在使用最新版本 9.4.1 的 Web 和 Javascript 和 Jquery。 I have the following code where I check if the user is currently logged in and if yes then he is not allowed to access the login page.我有以下代码,用于检查用户当前是否已登录,如果是,则不允许他访问登录页面。 Similarly, if the user is logged in then I also hide the "Sign Up" and "Sign In" buttons and display the "Sign Out" button.同样,如果用户已登录,我也会隐藏“注册”和“登录”按钮并显示“退出”按钮。 However, during my experiment I found out that these actions are performed quite slowly.然而,在我的实验中,我发现这些动作的执行速度非常缓慢。 This means that if I try to access the login page then the page loads for nearly half a second, displays all the contents of the page (login form and stuffs) in a flash for half a second and then decides to redirect back to the index page.这意味着,如果我尝试访问登录页面,那么页面会加载近半秒,在半秒内快速显示页面的所有内容(登录表单和内容),然后决定重定向回索引页。 Even the "Sign Up" and "Sign In" buttons are displayed for the similar amount of time before getting hidden and "Sign Out" button takes time to display too.甚至“注册”和“登录”按钮在隐藏之前的显示时间也相似,而“退出”按钮也需要时间来显示。 Why these delays are there?为什么会有这些延迟? Is there any way I can eliminate these delays and make the actions be performed instantly?有什么方法可以消除这些延迟并立即执行操作?

Authentication checking code认证校验码

onAuthStateChanged(auth, (user) => {
  if(user){
    const uid = user.uid;
    (window.location.pathname.split("/").pop() == 'login.php')?location.replace("index.php"):null;
    $(".logout").show();
    $(".login").hide();
    $(".register").hide();
  }
});

When the page loads Firebase tries to restore the user's authentication state from the information it has in local storage.当页面加载时,Firebase 尝试从本地存储中的信息恢复用户的身份验证状态。 This requires that it makes a call to server to check the credentials, and things like whether the account is disabled.这要求它调用服务器以检查凭据,以及帐户是否被禁用等。 This call takes some time, and only once the call completes will your onAuthStateChanged be called with the result.此调用需要一些时间,并且只有在调用完成后才会使用结果调用您的onAuthStateChanged

This means that the short flash you see is quite normal, and can not be prevented with just the Firebase API.这意味着您看到的短闪是很正常的,仅靠 Firebase API 无法阻止。 A trick you can do is store a small token value in local storage yourself, and then reading that right as the page loads to determine whether the user is likely to be authenticated.您可以做的一个技巧是自己在本地存储中存储一个小的令牌值,然后在页面加载时读取该值以确定用户是否可能通过身份验证。 Michael Bleigh explained this technique in his Google I/O talk Architecting Mobile Web Apps Michael Bleigh 在他的 Google I/O talk Architecting Mobile Web Apps 中解释了这种技术

While you cannot prevent the flash in all cases with this, you can use it to prevent the flash for most users effectively.虽然您不能在所有情况下都使用此方法来防止闪光,但您可以使用它来有效地防止大多数用户出现闪光。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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