简体   繁体   English

我正在为我的应用程序使用Firebase身份验证。 似乎有一个无限循环,页面不断被重定向

[英]I am using Firebase authentication for my application. There seems to be an infinite loop where the page keeps being redirected over and over again

The following function is to redirect the user to "Select Stream.html" when the user logs in.It keeps on replacing the location over and over again. 以下功能是在用户登录时将用户重定向到“Select Stream.html”。它会一直反复替换位置。

firebase.auth().onAuthStateChanged(function(user) {
    if (user) {
        window.location.replace("Select Stream.html");
      // User is signed in.
    } else {
      // No user is signed in.
      window.location.replace("index.html");
    }
  });

I am new to coding. 我是编码的新手。

 Here is the Log in Function function login() { var userEmail=document.getElementById("email-field").value; var userPassword=document.getElementById("password-field").value; firebase.auth().signInWithEmailAndPassword(userEmail, userPassword).catch(function(error) { // Handle Errors here. var errorCode = error.code; var errorMessage = error.message; /* window.location='Select Stream.html'; */ window.alert("Error : " + errorMessage); // ... }); } The Login function will be triggered when the signin-button is clicked. 
 <input onclick="login()" type="submit" name="signin-button" value="Sign In"/> 

First, change the name of Select Stream.html to have no spaces or capital letters. 首先,将Select Stream.html的名称更改为没有空格或大写字母。 This is common practice, and I recommend changing the name of the file to select_stream.html . 这是常见做法,我建议将文件名更改为select_stream.html

Before opening select_stream.html or index.html , check whether the user is already on that page to prevent the page from refreshing, like this: 在打开select_stream.htmlindex.html之前,请检查用户是否已经在该页面上以防止页面刷新,如下所示:

if (user) {
  // User is signed in.
  if(window.location.href.indexOf("select_stream.html") == -1){
    window.location.replace("select_stream.html");
  }
} else {
  // No user is signed in.
  if(window.location.href.indexOf("index.html") == -1){
    window.location.replace("index.html");
  }
}

The window.location.href variable refers to the URL of the current page, and the .indexOf function allows you to check if a value is contained inside the URL. window.location.href变量引用当前页面的URL,而.indexOf函数允许您检查URL中是否包含值。 The .indexOf function returns -1 if the specified value could not be found within the string, so this code simply only redirects if the user is not already on the redirect page. 如果在字符串中找不到指定的值,则.indexOf函数返回-1 ,因此,如果用户尚未在重定向页面上,则此代码仅重定向。

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

相关问题 为什么不将我重定向到新页面? - Why am I not being redirected to a new page? 当我使用反应钩子时,API 被一遍又一遍地调用 - API is getting called over and over again when I am using react hooks .on事件不断被解雇 - .on event keeps getting fired over and over again 我是否在此jQuery代码中一再绑定事件? - Am I binding events over and over again in this jQuery code? 为什么在尝试循环下一个 js 页面中的数据(使用 getStaticProps)时我得到未定义? - Why am I getting undefined when attempting to loop over data in next js page (using getStaticProps)? 我正在使用Firebase身份验证,但是当我重定向到其他页面时,我没有得到(uid) - I'm using firebase authentication but when I'm redirected to other page I'm not getting (uid) 为什么我会被重定向到此联系表单中的 php 文件? - Why am I being redirected to my php file in this contact form? 单击div元素后为什么会被重定向? - Why am I being redirected after clicking on my div element? 功能开始一遍又一遍地重复 - Function starts being repeated over and over again 当我尝试循环遍历一个字符串时,在反应中陷入无限循环 - Falling into an infinite for loop in react when I try to loop over a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM