简体   繁体   English

Firebase Web重定向用户到登录页面

[英]Firebase Web redirect user to login page

Trying to simply redirect the users who are authenticated to home.html and those who are not to login.html via Firebase. 试图简单地重定向谁被认证的用户home.html和那些谁不来login.html通过火力地堡。

firebase.auth().onAuthStateChanged(function (user) {
  if (user) {
    console.log('SIGNED IN');
    window.location.href = "home.html"; // <-- This is not good...
  } else {
    console.log('NOT SIGNED IN');
  }
});

Everyone says to use this bit of code, and it does work to keep track if the user is logged in, but if I put that line inside the if check it will just cause an infinite loop constantly redirecting the browser to the home page once the user is authenticated. 每个人都说要使用这段代码,并且可以确实跟踪用户是否已登录,但是如果我将该行放在if检查中 ,它将只会导致无限循环,不断地将浏览器重定向到主页。用户已通过身份验证。

Is there something simple I missed? 我错过了一些简单的事情吗?

The code you're showing will redirect any time the auth state changes to show there's a user present. 你展示的代码会重定向任何时候AUTH状态发生变化,显示有用户存在。 And that will trigger in any page being loaded, as the SDK doesn't know ahead of the page load if the user is logged in. The SDK has to wait for an init to complete in order to trigger the listener. 这将触发任何页面的加载,因为如果用户登录,SDK不会在页面加载之前知道。SDK必须等待初始化完成才能触发侦听器。 So, it sounds like that's not what you want. 因此,听起来这不是您想要的。

What you'll have to do is define specifically at what point in your app that you want a redirect to happen, and make sure that condition and timing is met before you redirect. 您需要做的是专门定义在应用程序中什么时候进行重定向,并确保在重定向之前满足条件和时间。

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

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