带有 sessionStorage 的 Firebase 身份验证不会在重新加载时加载会话

时间:2021-02-07 21:26:23

标签: firebase firebase-authentication

我已经设置了一个 firebase 网络应用程序,如果用户使用用户名和密码登录,它会将会话存储在 SessionStorage 中,如下所示:

 ...
 login(email, password): Promise<firebase.User | void> {
     return this.auth.setPersistence(firebase.auth.Auth.Persistence.SESSION)
         .then(() => this.auth.signInWithEmailAndPassword(email, password))
         .then((userCredential) => {
             // Signed in
             this.user = userCredential.user;

             return this.user;
         })
         .catch((error) => {});
  }

如果我登录,我可以看到用户对象存储在 SessionStorage 中。到目前为止一切顺利!

现在,当我重新加载页面时,firebase 似乎不会自动检索该对象。我想这是有道理的,因为 firebase 对 SessionStorage 一无所知。所以我想,也许我可以在 webApp 初始化期间再次执行 setPersistence

 this.auth.setPersistence(firebase.auth.Auth.Persistence.SESSION)
  .then(() => this.auth.currentUser)
  .then(user => {
       console.log(user); // -> null
  });

但是无论我做什么,firebase 都不会取回这个对象。所以我的问题是:我该怎么做才能告诉 firebase 从 SessionStorage 检索会话?

0 个答案:

没有答案
相关问题