回到我使用Parse时,看起来SDK会在本地存储会话数据,用户不必在刷新页面(或退出移动应用程序)后再次登录。Firebase/Angularfire的情况似乎并非如此;每次我刷新我的网页时,身份验证数据都会得到很多。这似乎是真正的基本和重要的功能,我会感到惊讶的是,了不起的人在Firebase还没有实现。我是不是遗漏了什么?
为了完整起见,下面是我在app.run()中的代码:
// ASG june 2016 - Upgrade firebase SDK
firebase.initializeApp(FirebaseConfig);
// login as anonymous if not already logged in
var currentUser = $firebaseAuth().$getAuth();
if (currentUser) {
console.log("Signed in as:", currentUser);
} else {
console.log("Not logged in; going to log in as anonymous");
$firebaseAuth().$signInAnonymously().then(function(authData) {
console.log("Signed in anonymously as:", authData.uid);
}).catch(function(error) {
console.error("Anonymous authentication failed:", error);
});
}
// register the on auth callback
$firebaseAuth().$onAuthStateChanged(function(authData) {
if (authData) {
console.log("Logged in as:", authData.uid);
if(typeof($rootScope.userProfile) == "undefined"){
$rootScope.userProfile = FirebaseProfileService.getUserProfile(authData.uid, false);
}
}
});发布于 2016-06-18 14:05:17
auth令牌在页面/应用程序重新加载之间持久化。但是,当页面重新加载时,它通常需要获得一个新的刷新令牌,这需要往返到Firebase服务器。因为这需要时间,所以初始getAuth()可能会返回null,而该过程正在进行。
var currentUser;
$firebaseAuth().$onAuthStateChanged(function(authData) {
if (authData) {
console.log("Logged in as:", authData.uid);
currentUser = authData.currentUser;
if(typeof($rootScope.userProfile) == "undefined"){
$rootScope.userProfile = FirebaseProfileService.getUserProfile(authData.uid, false);
}
}
else {
console.log("Not logged in; going to log in as anonymous");
currentUser = null;
$firebaseAuth().$signInAnonymously().catch(function(error) {
console.error("Anonymous authentication failed:", error);
});
}
});https://stackoverflow.com/questions/37895409
复制相似问题