不能具有永久性Google身份验证(Firebase)

时间:2019-05-22 23:25:30

标签: javascript firebase google-authentication

我正在尝试通过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);
});

这基本上是文档中编写的内容,应该非常简单。我真的不知道我在想什么。

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

谢谢!

1 个答案:

答案 0 :(得分:0)

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

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

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