Firebase在网页之间传递身份验证

时间:2016-05-31 16:00:13

标签: javascript firebase firebase-authentication

我不知道如何在我的网络之间共享身份验证。例如,我想在用户登录login.html后将身份验证传递给主页。

问题是什么:

用户登录login.html后,将创建身份验证。

//login.html
var user = firebase.auth().currentUser;
if (user) {
  // User is signed in.
}

但是,我需要在用户登录后将location.href更改为home.html。

//login.html
if (user) {
    // User is signed in.
    location.href = "home.html";
}

那么,更改网页时保持身份验证的解决方案是什么?

  //home.html
    if (user) {
        // 
    }else {
        // No user is signed in.
     } 

web / login.html< - >网/ home.html做为

1 个答案:

答案 0 :(得分:2)

Firebase通过将会话ID存储在本地存储中来跟踪页面之间的身份验证状态。

确保用户顺畅流动的最佳方法是monitor the authentication state。从该文件:

  

获取当前用户的推荐方法是在Auth对象上设置观察者:

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

通过使用观察者,您确保Auth对象不处于中间状态 - 例如初始化 - 当您获得当前用户时。