Javascript 页面重定向到另一个页面(仪表板)后,如何使用firebase身份验证用户数据?
使用firebase身份验证使用电子邮件和密码登录后,页面将重定向到用户仪表板,页面重定向后如何使用该仪表板中的用户凭据Javascript 页面重定向到另一个页面(仪表板)后,如何使用firebase身份验证用户数据?,javascript,firebase,firebase-authentication,Javascript,Firebase,Firebase Authentication,使用firebase身份验证使用电子邮件和密码登录后,页面将重定向到用户仪表板,页面重定向后如何使用该仪表板中的用户凭据 firebase.auth().signInWithEmailAndPassword(this.email, this.password) .then(cred => { console.log(cred.user); w
firebase.auth().signInWithEmailAndPassword(this.email, this.password)
.then(cred => {
console.log(cred.user);
window.location='dashboard.html'
}).catch(err => {
this.feedback=err.message;
})
使用给定命令将数据存储在cloud firestore中:-
firebase.auth().createUserWithEmailAndPassword(this.email, this.password)
.then(cred => {
return db.collection('students').doc(cred.user.uid).set({
name: this.name,
email: this.email
});
}).then(() => {
this.feedback='User registered, please login.';
window.location='login.html' ;
})
.catch(err => {
this.feedback=err.message;
});
我想使用数据库中用户uid索引上的存储详细信息
并将其显示在用户仪表板上
firebase.auth().onAuthStateChanged(user => {
if (user) {
console.log(user.email);
window.location= 'dashboard.html'
console.log(user.email);
this.name = user.displayName ;
this.email =user.email;
}
})
要让用户进入新页面,请使用
onAuthStateChange
侦听器,如图所示:
在
else
块中,您通常会重定向回登录页面。由于Firebase身份验证会自动刷新其短暂的ID令牌,因此在仪表板页面中包含上述代码也意味着用户可以安全地将此仪表板页面的URL添加到书签中,并在稍后返回。您是否尝试过使用本地存储
?到目前为止,您尝试过什么?我对所有这些都是新手,因为现在我什么都没有尝试过。当问堆栈溢出问题时,最好是亲自尝试解决这个问题。否则,感觉我们是在为你做工作,而不是在你真正陷入困境的地方帮助你。对不起,我的意思是我尝试过使用当前用户,但当登录页面重定向到其他页面时,用户已注销或为空。因此,我无法理解用户如何不注销不同的URL,因为您添加到问题中的onAuthStateChanged
代码位于仪表板文件中,加载仪表板后它会显示什么日志输出。请将浏览器JavaScript控制台的确切输出复制/粘贴到您的问题中。我使用了相同的onAuthStateChange侦听器,但每当登录后页面重定向到仪表板时,控制台中的登录用户凭据都显示为空。(每次页面重定向时,用户都会注销)。您共享的代码不显示onAuthStateChanged
侦听器,因此无法说明为什么它不适合您。我已编辑了代码并添加了onAuthStateChanged
侦听器。所以,我希望你现在能帮忙。
firebase.auth().onAuthStateChanged(function(user) {
if (user) {
// User is signed in.
} else {
// No user is signed in.
}
});