简体   繁体   English

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

[英]authData is null after redirecting to another page

I have two small web apps and I'm redirecting from the first one to the second one after a login with Firebase.我有两个小型网络应用程序,在使用 Firebase 登录后,我正在从第一个重定向到第二个。 My current problem is that the Authentication data is not saved and get's null after the new page is loaded.我当前的问题是在加载新页面后,未保存身份验证数据并且获取为空。

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

So, first I'll get logged in correctly and authData shows the login details, but on the new page http://localhost:3000/ authData is null.因此,首先我会正确登录并且authData显示登录详细信息,但在新页面上http://localhost:3000/ authData为空。

Does anyone know how to keep the session?有谁知道如何保持会话? I played around with the remember object but it didn't solve my problem.我玩弄了remember对象,但它没有解决我的问题。

Firebase Authentication automatically persists the session in the browser's local storage. Firebase 身份验证会自动将会话保留在浏览器的本地存储中。 So when you get to the new page, the user is already authenticated.因此,当您进入新页面时,用户已通过身份验证。

But your code doesn't detect this, because you're only handling the case where you actively authenticate the user.但是您的代码不会检测到这一点,因为您只处理主动验证用户身份的情况。

The solution is to also monitor the authentication state .解决方案是监视身份验证状态 From that documentation:从该文档:

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

If you put this snippet in the new page, it will detect that the user is already signed in.如果您将此代码段放在新页面中,它将检测到用户已登录。

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

相关问题 在加载另一个页面后重定向到一个页面之后 - After Redirecting to a page after loading another page 使用 history() 从另一个页面重定向后,react state 设置为 null,稍后更新为正确的值 - Firebase - react state sets to null after redirecting from another page using history() and later updates to the correct value - Firebase React.js重定向到另一个页面 - Reactjs redirecting to another page 重定向到Cordova应用程序中的另一个页面后无法运行javascript - Can't run javascript after redirecting to another page in cordova app 重定向后如何显示另一个页面的隐藏div - How to show hidden div of another page after redirecting 使用谷歌登录后重定向到另一个页面 - Firebase Web - Redirecting to another page after sign in with google - Firebase Web JavaScript:函数()在重定向到另一个页面后未执行 - JavaScript: function () not executed when redirecting to another page after it 将用户重定向到另一个网页后显示消息 - displaying a message after redirecting the user to another web page 单击提交按钮后重定向到另一个页面并传递变量 - Redirecting to another page and passing variables after clicking a submit button 将此人重定向到另一个页面后执行点击 function - Executing a click function after redirecting the person to another page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM