Fire base auth基于用户重定向页面

时间:2018-04-28 17:05:50

标签: javascript html firebase firebase-authentication

我目前是一年级学生,我正在创建Web应用程序。我已设法使用window.location = ""将页面重定向到主菜单。

但是,我想要多个重定向页面,例如admin用户可以访问他们自己的页面,以及新用户可以访问他们的页面,最后员工将转到他们自己的页面。

我当前的功能在js文件中,主要代码在HTML文件中。有人能帮帮我吗?

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
    window.location = "main.html"

    var user = firebase.auth().currentUser;

    if(user != null){
      var email_id = user.email;
    }

  } else {
    // No user is signed in.
    document.getElementById("user_div").style.display = "none";
    document.getElementById("login_div").style.display = "block";
  }
});

function login(){

  var userEmail = document.getElementById("email_field").value;
  var userPass = document.getElementById("password_field").value;

  firebase.auth().signInWithEmailAndPassword(userEmail, userPass).catch(function(error) {
    // Handle Errors here.
    var errorCode = error.code;
    var errorMessage = error.message;

    window.alert("Error : " + errorMessage);

    // ...
  });
}

function logout(){
  firebase.auth().signOut();
}
<html>
<head>
  <title>Diabetes Login</title>
  <link href="https://fonts.googleapis.com/css?family=Nunito:400,600,700" rel="stylesheet">
  <link rel="stylesheet" href="css/style4.css" />
</head>
<body>

  <div id="login_div" class="main-div">
    <h3>Welcome to Diabetes log</h3>
    <input type="email" placeholder="Email..." id="email_field" />
    <input type="password" placeholder="Password..." id="password_field" />

    <button onclick="login()">Login to Account</button>
  </div>

  <div id="user_div" class="loggedin-div">
    <h3>Welcome User</h3>
    <p id="user_para"> You're currently logged in.</p>
    <button onclick="logout()">Logout</button>
  </div>

  <script src="https://www.gstatic.com/firebasejs/4.8.1/firebase.js"></script>
  <script>
    // Initialize Firebase
    var config = {
    apiKey: "AIzaSyBHP48kjdW-YzKCdZA_-TcVnni-NlCZZ8Q",
    authDomain: "diabetesapp-ee8fa.firebaseapp.com",
    databaseURL: "https://diabetesapp-ee8fa.firebaseio.com",
    projectId: "diabetesapp-ee8fa",
    storageBucket: "diabetesapp-ee8fa.appspot.com",
    messagingSenderId: "769133104242"
    };
    firebase.initializeApp(config);
  </script>
  <script src="js/index.js"></script>

</body>
</html>

0 个答案:

没有答案