Javascript 在多个页面上保持连接
我在让一个用户帐户连接到多个页面时遇到问题,我想做的是,当用户进行身份验证时,他会被重定向到另一个页面,并加载数据,但我无法做到这一点,因为他们不会将数据上载到其他页面 未加载数据: 我的代码js:Javascript 在多个页面上保持连接,javascript,firebase,firebase-authentication,Javascript,Firebase,Firebase Authentication,我在让一个用户帐户连接到多个页面时遇到问题,我想做的是,当用户进行身份验证时,他会被重定向到另一个页面,并加载数据,但我无法做到这一点,因为他们不会将数据上载到其他页面 未加载数据: 我的代码js: var authEmailPassButton = document.getElementById('authEmailPassButton') var logOutButton = document.getElementById('logOutButton') var emailInput =
var authEmailPassButton = document.getElementById('authEmailPassButton')
var logOutButton = document.getElementById('logOutButton')
var emailInput = document.getElementById('emailInput')
var passwordInput = document.getElementById('passwordInput')
authEmailPassButton.addEventListener('click', function(){
firebase
.auth()
.signInWithEmailAndPassword(emailInput.value, passwordInput.value)
.then(function(result){
console.log(result)
alert('Welcome')
})
.catch(function (error){
console.error(error.code)
console.error(erro.message)
alert('Failed to authenticate, check the console for the error!')
})
})
firebase.auth().onAuthStateChanged(function(user){
if (user) {
var user = firebase.auth().currentUser;
if (user != null) {
var email = user.email;
var emailVerified = user.emailVerified;
var uid = user.uid
displayName.innerText = 'Welcome, ' + email
window.location = 'home.html'
}
} else {
console.log('Not connected')
}
})
logOutButton.addEventListener('click', function(){
firebase
.auth().signOut()
.then(function (){
alert('You disconnected')
}, function(error){
console.error(error)
})
})
我的html代码:
火基
您没有经过身份验证
浏览器不会在显示的页面之间保持任何状态。但是,当Firebase从同一域加载新页面时,它将尝试恢复用户的身份验证状态
如果希望多页应用程序中的每个页面都了解登录用户,则需要在每个页面中添加一个onAuthStateChanged
侦听器
您拥有的侦听器在其他页面上也可以正常工作,不过我会将其简化为:
firebase.auth().onAuthStateChanged(function(user){
if (user) {
var email = user.email;
displayName.innerText = 'Welcome, ' + email
...
} else {
console.log('Not signed in')
}
})
你能详细解释一下“他们没有上传到其他页面的数据”是什么意思吗?您共享的代码中有哪一行没有达到您期望的效果?我想在另一个页面加载onAuthStateChanged上的数据,这意味着您需要在另一个页面上使用相同的
onAuthStateChanged
处理程序。我不知道,谢谢您的想法!!