在Firebase上登录后路由

问题描述:

因此,这是我尝试编辑我以前的帖子后再花两天时间并进行一些试验和错误。在Firebase上登录后路由

更具体地说,我需要帮助的是根据输入的电子邮件或管理员标识号路由到特定页面。您现在登录后,firebase会验证您在数据库中有一个帐户,并且此功能底部的while循环会被设置为正确的地图。

但是,如果您使用数据库中不存在的电子邮件登录,那么错误处理将提醒一个错误,但它不能做更多的事情。 IT不能做我需要做的事情,那就是断开或返回,并停止该功能 - 我假设它不能这样做,因为它的范围不允许它影响整个功能。我试图把while循环放在错误处理的条件中,但是似乎没有做任何事情,因为当我尝试登录时什么也没有发生。我甚至尝试使用一个变量来添加(+1),然后如果它多于0而不运行while循环,但是我记得错误处理的范围实际上不会改变全局变量,因此它无法访问该变量。

这让我发疯 - 我只是需要dam代码来运行while循环,如果errorCode或errorMessage出现的话。请代码领主给我一些指导。

代码,因为它代表...

<!-- <a ="btn-signup" href="#signup">sign up </a> --> 
    <div class="btn-signin" id="btn-signin"> 
    <form action="javascript: submitform()" method="get" id="myForm"> 
    <input type="email" name="email" id="emailInput" placeholder="Enter School Email"> 
    <input type="password" name="password" id="passwordInput" placeholder="Enter Password"> 
    <input type="text" name="uniqueID" id="myAdminID" placeholder="Admin ID *if applicable*"> 
    <input type="submit" id="emailSubmit" value="Submit">  
    </form> 
    </div> 
    <!-- Routing based on email ending "@fordham.edu" --> 
    <script type="text/javascript"> 
    function submitform() { 
     var email_input = document.getElementById("myForm").elements.namedItem("email").value; 
     var password_input = document.getElementById("myForm").elements.namedItem("password").value; 
     var adminID = document.getElementById("myForm").elements.namedItem("uniqueID").value;   

     if ((email_input.indexOf('.edu') >= 0) && (password_input.length > 4)) { 
     firebase.auth().signInWithEmailAndPassword(email_input, password_input).catch(function(error) { 
      // Handle Errors here. 
      var errorCode = error.code; 
      var errorMessage = error.message; 
      if(errorCode === "auth/user-not-found") { 
       alert("User does not exist - go ahead and sign up first");     
      } else if(errorCode === "auth/wrong-password") { 
       alert("You might have the wrong password, try again"); 
      }   
      // ... 
     });     
     } else if ((adminID.indexOf('GJGBING') >= 0) && (password_input.length > 4)) { 
     firebase.auth().signInWithEmailAndPassword(email_input, password_input).catch(function(error) { 
      // Handle Errors here. 
      var errorCode = error.code; 
      var errorMessage = error.message; 
      if(errorCode === "auth/user-not-found") { 
       alert("User does not exist - go ahead and sign up first"); 
      } else if(errorCode === "auth/wrong-password") { 
       alert("You might have the wrong password, try again"); 
      }      
      // ... 
     });     
     } else { 
     alert("Please use a .edu email address or enter your unique Admin ID"); 
     return; 
     }; 


     var schools = ["@fordham.edu", "@sjsu.edu"]; 
     var mapLocations = ["#select-a-machine", "#select-a-machine"]; 
     var adminIdLocation = ["#select-a-machine"] 
     var adminIDs = ["GJGBING"]   
     var i = 0; 
     var a = 0; 

     while (a < adminIDs.length) { 
      if (adminID.indexOf(adminIDs[a]) >= 0) { 
       window.location.assign(adminIdLocation[a]); 
       break; 
      } else if (adminID.indexOf(adminIDs[a]) < 0) { 
       a++; 
      } 
     }; 


     while (i < schools.length) { 
      if(email_input.indexOf(schools[i]) >= 0) { 
       window.location.assign(mapLocations[i]); 
       break; 
      } else if(email_input.indexOf(schools[i]) < 0) { 
       i++; 
      } 
     }; 

     if(i >= schools.length && (email_input.indexOf('.edu') >= 0)) { 
      alert("We are currently not at your school"); 
     }; 

    }; 
    </script> 

使用:嵌套条件的两个层次内时

return; 

休息不工作。下面是我用来证明它的测试:

function test(){ 
for (i = 0; i < 10; i++) { 
    console.log(i); 
    if (i >= 5){ 
     console.log("great than five"); 
     while(i == 5){ 
      break; 
      console.log("break stops this conditional loop the function"); 
     } 
    } 
} 
    console.log("this will execute"); 
} 

凡相比,这种用法的回报,它杀死了整个功能:

function test(){ 
for (i = 0; i < 10; i++) { 
    console.log(i); 
    if (i >= 5){ 
     console.log("great than five"); 
     while(i == 5){ 
      return; 
      console.log("return kills this function, this won't log"); 
     } 
    } 
} 
    console.log("this won't execute"); 
} 
+0

我应该在哪里放 - 我把它在条件内激活,如果有错误抛出?我试着在提醒前后放置它,但在两种情况下,当我尝试提交表单时,它什么都不做。在控制台中,我看到与它有关的两个错误; “submitform未定义”和未捕获的SyntaxErrror:意外令牌。 – sergio

+0

我测试了这一点,我将编辑我的答案,该线程错误地说,将工作。较低的答案表明一个简单的回报应该起作用。由于它嵌套在两个级别的条件中,所以break不能以你拥有代码的方式工作。但我做了一个测试,我将在上面进行编辑,向您展示它的工作原理。 –

+0

所以我添加了返回到我的代码(检查出来的原始文章),但问题是,当凭证是正确的,它什么也没做。它不会提示错误消息或代码甚至路由。然而,如果用户不存在,它会提示一条错误消息,并保持它的位置,这是一个积极的。有什么想法吗? – sergio

如果此功能被使用的的onsubmit功能,那么您应在所需位置添加以下行:

return false; 

防止表单提交。见javascript: does onsubmit only execute when it detects a "return false"更多信息

编辑1: 我觉得你的问题是,您要添加到signInWithEmailAndPassword的执行误差函数()由火力点在发生错误的情况下执行。当您看到提示您注册的消息时,重新加载操作已经发生。您可以添加一条警报,显示 错误消息和firebase提供的代码。

如果你想在这种情况下,停止提交,取出location.reload if语句之前只是其他检查线路的errorCode的值,如果

 }); 
    if (errorCode) { 
     return false; 
    } 
    } else if (adminID.indexOf('GJGBING') >= 0) { 
+0

我编辑原始帖子,以显示它也链接的HTML也 – sergio

+0

正如我写的,我建议并使用'return false;'当你想中止提交函数 – Guenther

+0

的任何地方提交感谢解释 - 不幸的是它似乎没有工作。发生同样的事情。该警报被解雇告诉我,我应该注册,然后我被路由到不同的位置 – sergio