繁体   English   中英

认证后,firebase不会将用户重定向到另一个页面

[英]after authentication firebase is not re-directing the user to another page

我遇到的问题是,在Firebase中成功验证用户后(浏览器的调试控制台中没有错误),JS文件不会重定向到第二页。 我正在关注firebase提供的代码,并在github上搜索过。 在这一点上,我不确定我错过了什么。 提前谢谢你的帮助。

<!DOCTYPE html>
<html>
<head>
  <div>
    <title>My Page</title>
    <h1>test</h1>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="author" content="Name"
    <link href="https://fonts.googleapis.com/css?family=Nunito:400,600,700" rel="stylesheet">
    <link rel="stylesheet" href="style.css" />
  </div>
  <script src="https://www.gstatic.com/firebasejs/5.11.0/firebase-app.js"></script>
  <script src="https://www.gstatic.com/firebasejs/5.11.0/firebase-auth.js"></script>
  <script src="https://www.gstatic.com/firebasejs/5.11.0/firebase-database.js"></script>
  <script>
      var config = {
      apiKey: "",
      authDomain: "",
      databaseURL: "",
      projectId: "",
      storageBucket: "",
      messagingSenderId: ""
    };
    firebase.initializeApp(config);
  </script>
  <script src="webapp.js"></script>
</head>
<body>
  <div class="main-div">
    <h2>Test2</h2>
    <input id="txtEmail" type="email" placeholder="Email..." />
    <input id="txtPass" type="password" placeholder="Password..." />
    <button type="button" onclick="login()" >Login Here</button>
  <div>
</body>
</html>




.JS file

function login() {
  email = document.getElementById('txtEmail').value;
  password = document.getElementById('txtPass').value;
  loginBtn = document.getElementById('loginBtn');`enter code here`

firebase.auth().signInWithEmailAndPassword(email, password).catch(function(error) {
      var errorCode = error.code;
      var errorMessage = error.message;
      if (errorCode === 'auth/wrong-password') {
        alert('Wrong password, try again!.');
      } else {
        alert(errorMessage);
    }
      console.log(error);
});
}
function initApp() {
firebase.auth().onAuthStateChanged(function(user) {
  if(user) {
    // User is signed in
      window.location = 'secondpage.html';
 } else {
     alert('test!.');

}
}
)}

更换

function initApp() {
firebase.auth().onAuthStateChanged(function(user) {
  if(user) {
    // User is signed in
      window.location = 'secondpage.html';
 } else {
     alert('test!.');

}
}
)}

firebase.auth().onAuthStateChanged(function(user) { if(user) { window.location.href = 'secondpage.html'; } else { alert('test!.'); } } )

您需要将代码块移到函数之外( initApp()

暂无
暂无

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

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