【问题标题】:Firebase 3. authentication doesn't persistFirebase 3. 身份验证不会持续存在
【发布时间】:2023-03-19 14:39:02
【问题描述】:

我正在使用电子邮件和密码身份验证在 angularJS 应用程序中使用 firebase。 我只是将我的应用程序从 Firebase 2.x 更新到 3.x,并将 AngularFire 从 1.x 更新到 2.x。

我按照这 2 个文档进行迁移:

但是现在,每次刷新页面时,我都需要重新进行身份验证,就像没有持久会话一样。

我检查了 localStorage 密钥 firebase:authUser 我过去有一个 expirationTime(实际上它是用我登录的时间戳设置的)。

要检查用户是否已登录,我使用:$firebaseAuth().$getAuth()

编辑

这是我的问题的一个工作示例(登录名:toto@mail.fr/密码:toto123https://plnkr.co/edit/463Hse?p=preview

有人知道为什么会这样吗?

【问题讨论】:

标签: angularjs firebase firebase-authentication angularfire2


【解决方案1】:

我猜你是直接检查 currentUser 而不等待初始身份验证状态解决。你需要添加一个观察者:

firebase.auth().onAuthStateChanged(function(user) {
  if (user) {
    // User is signed in.
  } else {
    // No user is signed in.
  }
});

https://firebase.google.com/docs/auth/web/manage-users

此外,身份验证状态存储在网络存储中,因此请确保您已启用该功能。 (例如,状态不会在 safari 私人模式浏览中持续存在)。

【讨论】:

  • 好的,我试试这个。但这有点令人困惑,当我在 Firebase 2.x 和 Angularfire 1.x 中时,我使用了 $firebaseAuth().$getAuth() 而没有等待初始身份验证状态得到解决。在 angularfire 的迁移指南中,他们说“尽管您的所有承诺和 $getAuth() 调用将继续起作用,但身份验证负载会略有不同”。所以我想我无事可做。
  • 谢谢它的工作,我知道等待$waitForSignIn()返回他的承诺,然后再检查我的用户是否是auth并且它似乎工作。
猜你喜欢
  • 2016-10-20
  • 2018-04-18
  • 2013-08-19
  • 2017-10-05
  • 1970-01-01
  • 2013-08-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多