Firebase身份验证未在本地存储中存储数据

时间:2020-07-02 04:30:35

标签: javascript firebase firebase-authentication local-storage

当用户单击“提交”时,我的应用程序成功将用户登录到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";
    }
});

1 个答案:

答案 0 :(得分:0)

您必须使用localStorage

localStorage.setItem('userData', JSON.stringify(userDetail));

获取数据

JSON.parse(localStorage.getItem('userData'));

要删除

localStorage.removeItem('userData');