繁体   English   中英

重定向到另一个页面后 authData 为空

[英]authData is null after redirecting to another page

我有两个小型网络应用程序,在使用 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对象,但它没有解决我的问题。

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

但是您的代码不会检测到这一点,因为您只处理主动验证用户身份的情况。

解决方案是监视身份验证状态 从该文档:

// 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");
  }
});

如果您将此代码段放在新页面中,它将检测到用户已登录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM