javascript - 在多个页面上保持连接
问题描述
我无法将用户帐户连接在多个页面上,我想要做的是当用户进行身份验证时,他被重定向到加载数据的另一个页面,但我不能这样做,他们不是上传到其他页面。
我的代码js:
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:
<html>
<head>
<title>Firebase</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
<meta http-equiv="Content-Type" content="text/html"; charset="utf-8">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<link rel="stylesheet" href="/css/style.css">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
</head>
<body>
<div class="w3-bar w3-border w3-light-grey" style="background-color: white;">
<a href="#" class="w3-bar-item w3-button w3-green">Home</a>
<a href="#" class="w3-bar-item w3-button">News</a>
<a href="#" class="w3-bar-item w3-button">profile setting</a>
<a href="#" class="w3-bar-item w3-button">Form</a>
</div>
<h3 class="text-center" id="displayName">You are not authenticated</h3>
<!-- The core Firebase JS SDK is always required and must be listed first -->
<script src="https://code.jquery.com/jquery-3.5.1.js" integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.5/firebase-app.js"></script>
<script src="https://www.gstatic.com/firebasejs/7.14.4/firebase-database.js"></script>
<script src="/js/app.js"></script>
<script src="/js/authentication.js"></script>
</body>
</html>
我不知道为什么会这样!
解决方案
浏览器不会在它们显示的页面之间保持任何状态。但是,Firebase 会在从同一域加载新页面时尝试恢复用户的身份验证状态。
如果您希望多页面应用程序中的每个页面都知道登录用户,则需要onAuthStateChanged
为每个页面添加一个侦听器。
您拥有的侦听器看起来应该在其他页面上也可以正常工作,尽管我会将其简化为:
firebase.auth().onAuthStateChanged(function(user){
if (user) {
var email = user.email;
displayName.innerText = 'Welcome, ' + email
...
} else {
console.log('Not signed in')
}
})
推荐阅读
- file - 获取 IIS 日志记录中的内存使用情况
- apache-zookeeper - 是否可以避免嵌套的 RetryLoop.callWithRetry 调用,以便我获得一致的超时?
- r - 如何根据ggplot中的变量控制一行内的行大小?
- java - mybatis 没有返回延迟提取的所有行
- c# - 无法绑定多部分标识符“blah@blah.co.uk”。当使用 SqlCommand c#
- flutter - 如何维护 CupertinoSegmentedControl 的状态以实现国际化?
- macos - 在 MACOS 上使用 Catalyst 的 UIDocumentPickerViewController
- apache-kafka - 我可以在单个 mule 应用程序中有两个 kafka 消费者吗?都订阅了不同的主题
- python - matplotlib - 按数组的值着色
- python - sklearn.linear_model RidgeCV normalize= 参数究竟做了什么