Сейчас я учусь на первом курсе и создаю веб-приложение. Мне удалось перенаправить страницу в главное меню, используя window.location = ""
.
Однако я хочу, чтобы несколько страниц перенаправления, например, пользователь с правами администратора мог получить доступ к своей собственной странице, а новый пользователь мог перейти на свою страницу, и, наконец, сотрудник мог бы перейти на свою собственную страницу.
Мои текущие функции находятся в файле 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>