0

因此,我對網絡開發和Firebase也很陌生。我一直試圖在簡單的javascript和firebase中構建一個多頁面的網頁應用程序。應用程序看起來不錯,適用於大部分的部分。然而,這是沒有用的,真的,因爲我有以下問題:Firebase身份驗證有效,但不斷刷新頁面

  1. 當我通過googleAuthProvider登錄(我的index.html頁),我被帶到另一頁是main.html中。現在直到這裏都很好。但是一旦加載main.html,它會進入持續刷新的循環。

我的理由是,Firebase試圖在加載時重新驗證頁面。所以循環發生。但是,爲什麼,我無法調試。

我已經瀏覽了幾乎所有可以在互聯網上找到的東西,但沒有找到一個解決方案,這個解決方案是針對簡單的基於JavaScript的多頁面的Web應用程序。

如果有人對我的應用程序感興趣並且有足夠的興趣,可以點擊此鏈接。

Chatbot

而且,這裏是我的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(); 
 
\t 
 
var currentUser; 
 
var name; 
 
var photoUrl; 
 

 
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ \t 
 
\t initApp(); 
 
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
 
    \t if(signinGoogle){ 
 
\t \t googleAuth.addEventListener('click', e=>{ 
 
\t \t \t firebase.auth().signInWithPopup(new firebase.auth.GoogleAuthProvider()).then(function(result) { \t 
 
\t \t \t // This gives you a Google Access Token. You can use it to access the Google API. 
 
    \t \t \t var tokenGoogle = result.credential.accessToken; 
 
\t \t \t // The signed-in user info. 
 
\t \t \t var userGoogle = result.user; 
 
\t \t \t // ...Below line to be rmeooved if not working expectedly. 
 
\t \t \t \t // var user = firebase.auth().currentUser; 
 
\t \t \t }).catch(function(error) { 
 
    \t \t \t // Handle Errors here. 
 
    \t \t \t var errorCode = error.code; 
 
\t \t \t var errorMessage = error.message; 
 
\t \t \t // The email of the user's account used. 
 
\t \t \t var email = error.email; 
 
    \t \t \t // The firebase.auth.AuthCredential type that was used. 
 
    \t \t \t var credential = error.credential; 
 
    \t \t \t // ... 
 
\t \t \t }); 
 
\t \t }); 
 
\t \t 
 
\t \t } 
 
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
 
    \t \t if(signOut){ 
 
      signout.addEventListener('click', e=>{ 
 
\t \t  
 
\t \t if(confirm("Do you wish to leave?")){ 
 
\t \t \t promise = firebase.auth().signOut().then(function(){ 
 
\t \t \t window.location = "index.html"; 
 
\t \t \t }); 
 
\t \t \t promise.catch(e => 
 
\t   console.log(e.message)) 
 
\t \t \t } \t 
 
\t \t \t 
 
\t \t \t }); 
 
\t \t } 
 
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
 
    function initApp(){ 
 
    firebase.auth().onAuthStateChanged(function(user){ 
 
\t  
 
\t if(user){ 
 
\t window.location = "main.html"; 
 
\t 
 
\t $("document").ready(function(){ 
 
\t \t \t \t 
 
\t \t \t currentUser = firebase.auth().currentUser; 
 
\t \t   name = currentUser.displayName; 
 
\t \t \t photoUrl = currentUser.photoURL ; 
 
\t \t 
 
\t \t \t console.log("Current user's name is : "+name); 
 
\t \t \t console.log("Current user's photoUrl is : "+photoUrl); 
 
\t   
 
\t \t \t displayNAME.innerHTML = "Hi "+name; 
 
\t \t \t 
 
    //+++++++++++Retrieving Msgs++++++++++++++++++++++++++++++++ 
 
\t \t \t \t var i=1; \t 
 
\t \t \t \t var firebaseRetrieveRef = firebase.database().ref().child(name+uid+"/MessageBoard"); 
 
\t \t \t \t firebaseRetrieveRef.on("child_added", snap =>{ 
 
\t \t \t \t var retrievedMsg = snap.val(); 
 
\t \t \t \t console.log("retrieved msgs is : "+retrievedMsg); 
 
\t \t \t \t $("#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>"); 
 
\t \t \t \t i++; 
 
\t \t \t \t \t }); 
 
\t //+++++++++++Storing Msgs++++++++++++++++++++++++++++++++ 
 
\t \t $("#send").on("click", function(){ 
 
\t \t \t var newMessage=messageBox.value; 
 
\t \t \t if(newMessage==""){ 
 
\t \t \t alert("Empty Message doesn't make any sense, does it?? "); 
 
\t \t \t } 
 
\t \t \t else{ 
 
\t \t \t var firebaseStoreRef = firebase.database().ref().child(name+uid+"/MessageBoard"); 
 
\t \t \t firebaseStoreRef.push().set(newMessage); 
 
       messageBox.value=""; 
 
\t \t \t } 
 
\t \t \t }); 
 
\t //+++++++++++Clearing/deleting all tasks++++++++++++++++++++++++ 
 
\t \t $("#clear").on("click", function(){ 
 
\t \t \t var firebaseDeleteRef = firebase.database().ref().child(name+uid+"/MessageBoard"); 
 
\t \t \t firebaseDeleteRef.remove(); 
 
\t \t \t $(".scrolls").empty(); 
 
\t \t \t }); 
 
    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
 
\t 
 
\t }); 
 
       } 
 
\t \t else 
 
\t \t { 
 
\t \t console.log(user+" is not logged in"); 
 
\t \t } 
 
\t \t 
 
\t \t }); 
 
    } \t 
 
\t \t \t 
 
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
 
});

回答

0

你留着重定向到main.html中。

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

你留着重定向到main.html中,只要你確定用戶已登錄。確保在main.html中,您沒有使用相同的邏輯,再次重定向。

+0

我的main.html沒有任何javascript代碼,除了我的logic.js文件和firebase源代碼。 。所以,我仍然不清楚爲什麼這個循環正在發生。 –