[英]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.html
或index.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.