首页 文章

如何检索存储在Firebase中的用户的自动生成的密钥?

提问于
浏览
1

我有点卡住了 . 在我的代码中,我让用户通过Facebook注册/登录 . 我使用了一个Oauth令牌,这要归功于我用来登录用户的cordova facebook插件 .

如果用户不存在,我将一些Facebook信息(名称,电子邮件, Profiles 图片)添加到用户下的firebase数据库中 .

我的问题是如何检索此用户(不知道自动生成的密钥) . 假设用户在Facebook中修改了他的 Profiles 图片,因为我只在第一个连接处存储了数据,但它没有反映在我的应用程序中 .

我的数据库看起来像这样:

users: {
    -JtGR-24WUU2HVX3x5c3d: {
        image: http//
        name: joe
        email: cezd
    },
    -JtGR-24WUU2HVX3x5c43r: {
        image: http//
        name: john
        email:cezs
    }
}

谢谢您的帮助 .

阿尔诺

1 回答

  • 0

    这是你应该怎么做:

    创建帐户(并且用户已登录)后,您将可以通过 $firebaseAuth 服务访问其用户ID . 这是你如何做到这一点:

    // Get the unique id for the user
    var uid = $firebaseAuth(<your-firebase-ref>).$getAuth().uid;
    

    然后我们可以在这个 uid 变量下存储东西,允许我们轻松地为我们的帐户进行CRUD操作 . 这里我们在 uid 下有一个名为 programs 的数组,其中包含一些程序:

    // Specify the path we will be working with
    var path = uid + '/programs';
    

    接下来,我们可以使用我们刚刚定义的路径获取 programs 数组:

    // Get all programs, make sure to include a / after your reference 
    var programs = $firebaseArray(<your-firebase-ref> + path);
    

    当它完成加载后,将程序分配给 $scope ,以便我们可以在前端使用它 .

    programs.$loaded()
      .then(function(data) {
        $scope.programs = data;
      })
      .catch(function(err) {
        $scope.error = 'There was an error with the request';
    });
    

相关问题