【发布时间】:2020-10-22 14:55:27
【问题描述】:
当用户点击“提交”时,我的应用成功将用户登录到 Firebase,但 Firebase 不会将用户身份验证数据存储在 Chrome 的本地存储中。我检查了一下,Firebase 成功返回了用户对象,程序使用正确的用户 ID 打印“用户登录”。但是,当我在控制台中运行“localStorage”时,该对象不存在,并且用户身份验证不会持续存在。我不确定为什么它不起作用。这是我的代码:
firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL);
function authenticate(event){
event.preventDefault();
let email = document.getElementById("email").value;
let password = document.getElementById("password").value;
let result = firebase.auth().signInWithEmailAndPassword(email, password)
.then(
(user) => {
alert("User: " + JSON.stringify(user)); //this works
}
)
.catch(console.log);
}
firebase.auth().onAuthStateChanged( function(user){
if(user){
console.log("user signed in"); //this works
console.log("user id: " + firebase.auth().currentUser.uid);
window.location.href = "./calendar.html";
} else {
console.log("No user");
window.location.href = "./index.html";
}
});
【问题讨论】:
-
我也看到了同样的情况,你有想过这个吗?
-
如果浏览器支持 IndexedDB,firebase 将默认存储凭据而不是 localStorage(这是默认设置):stackoverflow.com/a/51029091/197472
-
当我在做这个项目时,我从来没有想通,最终我将我的网站重组为一个单页应用程序。但是,我刚刚再次打开该项目,由于某种原因,它现在似乎工作正常。现在,Firebase 正在设置 localStorage 并且每个页面都可以访问它。我不确定为什么它现在可以工作而以前没有。我仍然会尝试复制该错误,如果发现任何问题,我会回复您。
-
您是否使用过不支持 IndexedDB 的旧浏览器?显然 localStorage 是后备。
标签: javascript firebase firebase-authentication local-storage