重定向到另一个页面后 authData 为空
authData is null after redirecting to another page
我有两个小型 Web 应用程序,我在使用 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 身份验证自动将会话保存在浏览器的本地存储中。所以当你到达新页面时,用户已经通过身份验证。
但是您的代码没有检测到这一点,因为您只是在处理您主动验证用户身份的情况。
解决方法是也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");
}
});
如果您将此代码段放在新页面中,它将检测到用户已经登录。
我有两个小型 Web 应用程序,我在使用 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 身份验证自动将会话保存在浏览器的本地存储中。所以当你到达新页面时,用户已经通过身份验证。
但是您的代码没有检测到这一点,因为您只是在处理您主动验证用户身份的情况。
解决方法是也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");
}
});
如果您将此代码段放在新页面中,它将检测到用户已经登录。