繁体   English   中英

Firebase身份验证有效,但仍在刷新页面

[英]Firebase Authentication works but keeps on refreshing the page

因此,我还是Web开发和Firebase的新手。 我一直在尝试使用简单的javascript和firebase构建多页Web应用程序。 应用看起来不错,并且可以在大多数情况下使用。 但这确实没有用,因为我遇到以下问题:

  1. 当我通过googleAuthProvider登录(在index.html页面上)时,被带到另一个页面main.html。 现在直到这里都很好。 但是一旦加载main.html,它就会进入持续刷新的循环。

我这样做的理由是,Firebase尝试以某种方式在加载时重新验证页面。 这样循环就发生了。 但是为什么,这我无法调试。

我查看了几乎所有可以在互联网上找到的所有内容,但没有找到可以解决的简单的基于Firebase的基于JavaScript的多页面Web应用程序的解决方案。

如果有人有兴趣和友好的目光,这是我的应用程序的链接。

聊天机器人

另外,这也是我的JavaScript代码。

 var config = { apiKey: "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", authDomain: "XXXXXXXXX.firebaseapp.com", databaseURL: "https://XXXXXXXX.firebaseio.com", projectId: "XXXXXXXXXX", storageBucket: "XXXXXXXXXX.appspot.com", messagingSenderId: "XXXXXXXXXXXX" }; firebase.initializeApp(config); //=============================================================================================== $("document").ready(function(){ const signinGoogle = document.getElementById("googleAuth"); const signOut = document.getElementById("signout"); const sendMsg = document.getElementById("send"); const messageBox = document.getElementById("chatBox"); const displayNAME = document.getElementById("dipslayName"); const storageRef = firebase.storage().ref(); var currentUser; var name; var photoUrl; //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ initApp(); //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ if(signinGoogle){ googleAuth.addEventListener('click', e=>{ firebase.auth().signInWithPopup(new firebase.auth.GoogleAuthProvider()).then(function(result) { // This gives you a Google Access Token. You can use it to access the Google API. var tokenGoogle = result.credential.accessToken; // The signed-in user info. var userGoogle = result.user; // ...Below line to be rmeooved if not working expectedly. // var user = firebase.auth().currentUser; }).catch(function(error) { // Handle Errors here. var errorCode = error.code; var errorMessage = error.message; // The email of the user's account used. var email = error.email; // The firebase.auth.AuthCredential type that was used. var credential = error.credential; // ... }); }); } //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ if(signOut){ signout.addEventListener('click', e=>{ if(confirm("Do you wish to leave?")){ promise = firebase.auth().signOut().then(function(){ window.location = "index.html"; }); promise.catch(e => console.log(e.message)) } }); } //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ function initApp(){ firebase.auth().onAuthStateChanged(function(user){ if(user){ window.location = "main.html"; $("document").ready(function(){ currentUser = firebase.auth().currentUser; name = currentUser.displayName; photoUrl = currentUser.photoURL ; console.log("Current user's name is : "+name); console.log("Current user's photoUrl is : "+photoUrl); displayNAME.innerHTML = "Hi "+name; //+++++++++++Retrieving Msgs++++++++++++++++++++++++++++++++ var i=1; var firebaseRetrieveRef = firebase.database().ref().child(name+uid+"/MessageBoard"); firebaseRetrieveRef.on("child_added", snap =>{ var retrievedMsg = snap.val(); console.log("retrieved msgs is : "+retrievedMsg); $("#taskList").append("<li id='list"+i+"'><div style='width:100%'><img src='"+photoUrl+"'style='width:10px;height:10px;border-radius:5px;'/><label>"+name+"</label></div><div style='width:100%'><p>"+retrievedMsg+"</p></div></li>"); i++; }); //+++++++++++Storing Msgs++++++++++++++++++++++++++++++++ $("#send").on("click", function(){ var newMessage=messageBox.value; if(newMessage==""){ alert("Empty Message doesn't make any sense, does it?? "); } else{ var firebaseStoreRef = firebase.database().ref().child(name+uid+"/MessageBoard"); firebaseStoreRef.push().set(newMessage); messageBox.value=""; } }); //+++++++++++Clearing/deleting all tasks++++++++++++++++++++++++ $("#clear").on("click", function(){ var firebaseDeleteRef = firebase.database().ref().child(name+uid+"/MessageBoard"); firebaseDeleteRef.remove(); $( ".scrolls" ).empty(); }); //+++++++++++++++++++++++++++++++++++++++++++++++++++++++ }); } else { console.log(user+" is not logged in"); } }); } //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ }); 

您一直重定向到main.html。

firebase.auth().onAuthStateChanged(function(user){     
  if(user){
  window.location = "main.html";

每当您确定用户已登录时,您都会一直重定向到main.html。确保在main.html上,您没有使用相同的逻辑并再次重定向。

暂无
暂无

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

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