首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >重新加载Ionic应用程序/页面后的Firebase自动登录

重新加载Ionic应用程序/页面后的Firebase自动登录
EN

Stack Overflow用户
提问于 2016-06-18 09:28:45
回答 2查看 2K关注 0票数 2

回到我使用Parse时,看起来SDK会在本地存储会话数据,用户不必在刷新页面(或退出移动应用程序)后再次登录。Firebase/Angularfire的情况似乎并非如此;每次我刷新我的网页时,身份验证数据都会得到很多。这似乎是真正的基本和重要的功能,我会感到惊讶的是,了不起的人在Firebase还没有实现。我是不是遗漏了什么?

为了完整起见,下面是我在app.run()中的代码:

代码语言:javascript
复制
// 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); 
        }
    }
});
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-18 14:05:17

auth令牌在页面/应用程序重新加载之间持久化。但是,当页面重新加载时,它通常需要获得一个新的刷新令牌,这需要往返到Firebase服务器。因为这需要时间,所以初始getAuth()可能会返回null,而该过程正在进行。

代码语言:javascript
复制
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);
        });
    }
});
票数 1
EN

Stack Overflow用户

发布于 2016-06-18 12:27:16

Firebase绝对是authData的前身,它有很多优点,你在其他地方几乎找不到。因此,在这里我看不到调用getAuth()的任何逻辑原因,因为您可以很容易地从onAuthStateChanged侦听器获得onAuthStateChanged。在这种情况下,您可能需要删除使用getAuth()获取getAuth的操作,如果您希望以匿名的身份注册用户,我希望您可以将其他条件移动到onAuthStateChanged,这样就可以了。希望能成功。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37895409

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档