在我的网站中,默认页面是 index.html。如果用户未连接,则将他重定向到登录页面。当用户登录后,程序应该将他重定向回 index.html 并显示该页面。但是,当我登录时(并且凭据正确),它会再次将我重定向到登录页面。
这是索引页面的代码:
var user = firebase.auth().currentUser;
if (!user){
window.location = 'https://anastasispap.me/auth/login.html';
}
登录页面代码:
const loginForm = document.querySelector('#login-form');
loginForm.addEventListener('submit', (e) => {
e.preventDefault();
const email = loginForm['login-email'].value;
const password = loginForm['login-password'].value;
console.log(email, password)
auth.signInWithEmailAndPassword(email, password).then(cred => {
console.log(cred)
loginForm.reset()
window.location.assign("https://anastasispap.me/index.html")
})
})
感谢您的宝贵时间:)
最佳答案
firebase.auth().currentUser
在页面加载时不会立即填充当前用户。您应该附上 AuthStateListener查明用户对象是否可用以及何时可用,如 documentation 中所述.
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
} else {
// No user is signed in.
}
});
关于javascript - Firebase 身份验证在重定向时不进行身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58883467/