我正在开发一个使用Firebase作为我的后端的网站,用户已登录但登录后我无法转到下一页我需要它来取用户,它只是刷新登录页面。我有点教这个JavaScript,所以任何帮助都会很棒。
提前致谢。
登录页面:
<div class="container">
<div class="row">
<div class="col-md-3"></div>
<div class="col-md-6">
<p>Enter an email and password below and either sign in to an existing account or sign up</p>
<input class="mdl-textfield__input" style="display: inline; width: auto;" type="text" id="email" name="email" placeholder="Email" />
<input class="mdl-textfield__input" style="display: inline; width: auto;" type="password" id="password" name="password" placeholder="Password" />
<br />
<br />
<button class="mdl-button mdl-js-button mdl-button--raised" id="quickstart-sign-in" name="signin">Sign In</button>
</div>
<div class="col-md-3"></div>
</div>
</div>
<script src="https://www.gstatic.com/firebasejs/4.9.0/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.9.0/firebase-auth.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.9.0/firebase-database.js"></script>
<script src="https://www.gstatic.com/firebasejs/4.9.0/firebase.js"></script>
<script>
// Initialize Firebase
var config = {
//
};
firebase.initializeApp(config);
</script>
<script type="text/javascript">
/**
* Handles the sign in button press.
*/
function toggleSignIn() {
var email = document.getElementById('email').value;
var password = document.getElementById('password').value;
if (email.length < 4) {
alert('Please enter an email address.');
return;
}
if (password.length < 4) {
alert('Please enter a password.');
return;
}
// Sign in with email and pass.
// [START authwithemail]
firebase.auth().signInWithEmailAndPassword(email, password).catch(function (error) {
// Handle Errors here.
var errorCode = error.code;
var errorMessage = error.message;
// [START_EXCLUDE]
if (errorCode === 'auth/wrong-password') {
alert('Wrong password.');
} else {
alert(errorMessage);
}
console.log(error);
});
}
function initApp() {
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
alert("welcome");
window.location = '../UserPages/UserIndex.aspx';
}
});
document.getElementById('quickstart-sign-in').addEventListener('click', toggleSignIn, false);
}
window.onload = function () {
initApp();
};
</script>