Javascript Firebase身份验证加载页面的onAuthStateChanged中的重定向
我听从了老师的指示 index.html: auth.js: 我可以看出,这段代码是一个无止境的循环。但我看不出我从说明书中漏掉了什么。似乎此说明应该有效。当您调用该方法并成功时,用户已登录。所以,如果我正确理解了您的功能需求,您就不需要使用onAuthStateChanged 您可以按照以下思路做一些事情:Javascript Firebase身份验证加载页面的onAuthStateChanged中的重定向,javascript,firebase,firebase-authentication,Javascript,Firebase,Firebase Authentication,我听从了老师的指示 index.html: auth.js: 我可以看出,这段代码是一个无止境的循环。但我看不出我从说明书中漏掉了什么。似乎此说明应该有效。当您调用该方法并成功时,用户已登录。所以,如果我正确理解了您的功能需求,您就不需要使用onAuthStateChanged 您可以按照以下思路做一些事情: function login() { var email = document.getElementById('email').value; var password =
function login() {
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)
.then(userCredential => {
console.log("User " + userCredential.user.uid + " LOGGED IN");
window.location = 'admin-console.html';
})
.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);
//Throw an error
});
}
function logout() {
firebase.auth().signOut()
.then(() => {
console.log("User SIGNED OUT");
window.location = 'index.html';';
})
}
function initApp() {
console.log('start initApp');
document.getElementById('loginbutton').addEventListener('click', login, false);
document.getElementById('logoutbutton').addEventListener('click', logout, false);
}
window.onload = function () {
console.log('initialize app');
initApp();
};
请注意,您需要添加一个注销按钮。如果你只想有一个切换按钮,而不是一个登录按钮加上一个注销按钮,你可以很容易地修改上面的代码
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();
};
function login() {
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)
.then(userCredential => {
console.log("User " + userCredential.user.uid + " LOGGED IN");
window.location = 'admin-console.html';
})
.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);
//Throw an error
});
}
function logout() {
firebase.auth().signOut()
.then(() => {
console.log("User SIGNED OUT");
window.location = 'index.html';';
})
}
function initApp() {
console.log('start initApp');
document.getElementById('loginbutton').addEventListener('click', login, false);
document.getElementById('logoutbutton').addEventListener('click', logout, false);
}
window.onload = function () {
console.log('initialize app');
initApp();
};