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