【问题标题】:Can't have persistent Google authentication (Firebase)不能有持久的谷歌身份验证(Firebase)
【发布时间】:2019-05-22 23:25:30
【问题描述】:

我正在尝试使用 Firebase 进行持久的 Google 身份验证。我可以成功登录,但如果我重新加载页面,会话不会持续。

这是我的代码:

firebase.auth().setPersistence(firebase.auth.Auth.Persistence.LOCAL)
.then(function()
{
    let provider = new firebase.auth.GoogleAuthProvider();
    firebase.auth().signInWithPopup(provider)
    .then(function (result)
    {
        //Save auth data
    });
})
.catch(function(error)
{
    console.error(error);
});

这基本上是文档中所写的内容,应该非常简单。我真的不知道我错过了什么。

另外,我不知道当会话是永久的时会发生什么,它只是要静默进行身份验证还是要打开弹出窗口,进行身份验证然后自动关闭弹出窗口?

谢谢!

【问题讨论】:

    标签: javascript firebase google-authentication


    【解决方案1】:

    登录状态会自动保存到浏览器的本地存储中,并在页面/应用重新加载时恢复。要检测身份验证状态的变化,请使用onAuthStateChanged 侦听器,如here 所示:

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

    当用户主动登录时(即当对signInWithPopup 的调用完成时),以及在重新加载应用程序/页面后用户登录状态恢复时,此侦听器都会触发。

    【讨论】:

      猜你喜欢
      • 2018-04-18
      • 2018-01-01
      • 1970-01-01
      • 2020-10-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-30
      相关资源
      最近更新 更多