[英]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.