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

1 javascript web-applications firebase firebase-authentication firebase-storage

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

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

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

我已经查看了几乎可以在Internet上找到的所有内容,但没有找到可以针对具有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");
		}
		
		});
    }	
			  
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 });
Run Code Online (Sandbox Code Playgroud)

boj*_*eil 6

您一直重定向到main.html。

firebase.auth().onAuthStateChanged(function(user){     
  if(user){
  window.location = "main.html";
Run Code Online (Sandbox Code Playgroud)

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