簡體   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