【问题标题】:authData is null after redirecting to another page重定向到另一个页面后 authData 为空
【发布时间】:2016-09-07 14:32:39
【问题描述】:

我有两个小型网络应用程序,在使用 Firebase 登录后,我正在从第一个应用程序重定向到第二个应用程序。我当前的问题是在新页面加载后身份验证数据未保存并且为空。

var ref = new Firebase("https://xxx.firebaseio.com");
var credentials = {};
credentials.email = email;
credentials.password = password;

ref.authWithPassword(credentials, function(error, authData) {
  if (error) {
      console.log("Login Failed!", error);
      document.getElementById("login-status").innerHTML = error;
  } else {
    console.log("Authenticated successfully with payload:", authData);
    console.log("AuthData expires: " + authData.expires);
    window.location = "http://localhost:3000/";
  }
});

所以,首先我会正确登录,authData 显示登录详细信息,但在新页面上 http://localhost:3000/ authData 为空。

有人知道如何保持会话吗?我玩弄了remember 对象,但它并没有解决我的问题。

【问题讨论】:

    标签: javascript angularjs firebase firebase-authentication


    【解决方案1】:

    Firebase 身份验证会自动将会话保存在浏览器的本地存储中。因此,当您进入新页面时,用户已经通过身份验证。

    但是您的代码没有检测到这一点,因为您只是在处理主动对用户进行身份验证的情况。

    解决方案是monitor the authentication state。从该文档中:

    // Register the callback to be fired every time auth state changes
    ref.onAuth(function(authData) {
      if (authData) {
        console.log("User " + authData.uid + " is logged in with " + authData.provider);
      } else {
        console.log("User is logged out");
      }
    });
    

    如果你把这个sn-p放到新页面中,它会检测到用户已经登录了。

    【讨论】:

    • 我已经在新页面上使用了这个 sn-p,它给了我一个空值。也许问题在于页面运行在具有两个不同端口(3000 和 4000)的两个不同 Web 服务器上。如果我保留或重新加载其中一个页面,会话将被保存,但如果我更改为另一个页面(从 localhost:3000 到 localhost:4000),它会变为 null。
    • 啊,正如你所说,会话存储在浏览器的本地存储中,所以当我更改域时,会话丢失了。在一个域上部署后,我可以同时托管这两个 Web 应用程序,这样应该可以解决问题。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 2017-06-08
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    • 2011-06-02
    • 1970-01-01
    相关资源
    最近更新 更多