【发布时间】:2020-09-20 06:40:48
【问题描述】:
我是 firebase 新手,我不知道如何让用户在转到其他页面后保持登录状态。我能够成功登录并将用户数据发布到登录页面上的控制台,但是当我转到另一个页面并尝试使用 firebase.auth().currentUser。我最初在登录完成后立即将其重定向到另一个页面,然后将其关闭以查看我是否在信息中继到服务器之前离开该页面,但这并没有解决问题。我也尝试手动设置持久性无济于事。我环顾四周,没有找到任何解决问题的方法。
这是我的登录方法(与 firebase 文档上的基本相同),当点击表单上的登录按钮时调用:
function signIn(){
//getting credential input from form
var email = document.getElementById('email').value;
var password = document.getElementById('password').value;
console.log(email, password)
//calling firebase to sign in user
firebase.auth().signInWithEmailAndPassword(email, password)
.then(cred => {
//confirmation that user signed in
console.log("signed in");
}).catch(function(error) {
var errorCode = error.code;
var errorMsg = error.message;
if(errorCode === 'auth/wrong-password') {
alert('Wrong Password');
}
else {
alert(errorMsg);
}
console.log(error);
});
};
【问题讨论】:
-
.then(cred => { //confirmation that user signed in console.log("signed in");这里是用户输入。您要将登录用户的应用程序定向到哪里?
标签: javascript firebase firebase-authentication