【发布时间】:2020-01-29 10:58:48
【问题描述】:
我按照Stack Overflow - How to redirect after a user signs in with Firebase authentication?的指示进行操作
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My auth page</title>
<script defer src="/__/firebase/7.5.0/firebase-app.js"></script>
<script defer src="/__/firebase/7.5.0/firebase-auth.js"></script>
<script defer src="/__/firebase/7.5.0/firebase-database.js"></script>
<script defer src="/__/firebase/init.js"></script>
<link rel="stylesheet" href="style.css">
</head>
<body>
<!-- My html code -->
<script type="text/javascript" defer src="auth.js"></script>
</body>
</html>
auth.js:
function toggleSignIn() {
if (firebase.auth().currentUser) {
firebase.auth().signOut();
} else {
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;
}
firebase.auth().signInWithEmailAndPassword(email, password).catch(function (error) {
var errorCode = error.code;
var errorMessage = error.message;
if (errorCode === 'auth/wrong-password') {
alert('Wrong password.');
} else {
alert(errorMessage);
}
console.log(error);
});
}
}
function initApp() {
console.log('start initApp');
firebase.auth().onAuthStateChanged(function (user) {
if (user) {
console.log('logged in');
window.location = 'admin-console.html';
} else {
console.log('not logged in');
window.location = 'index.html'; //causes endless loop
}
});
document.getElementById('loginbutton').addEventListener('click', toggleSignIn, false);
}
window.onload = function () {
console.log('initialize app');
initApp();
};
我可以看到这段代码进入了一个无限循环。但我没有看到我从指令中遗漏了什么。看来这个说明应该可以了。
【问题讨论】:
标签: javascript firebase firebase-authentication