在firebase中获取经过身份验证的用户数据

时间:2016-05-08 23:16:39

标签: angularjs ionic-framework firebase

  1. 我有一个代码,用于在登录firebase后显示我的信息     但它只是推出了控制台日志,这不是我想要的     想要在我的离子列表视图中显示数据。
  2. 我如何确保authData仍然存在于我的内部 范围,即使我刷新浏览器
  3. 这是我的控制器代码:

    app.controller('LoginCtrl', function($scope, $state, $ionicPopup, $firebaseAuth, $firebaseArray, $firebaseObject){
    
    $scope.formData = { "email": "", "password": "" };
    
    $scope.currentUserName ={}; $scope.currentUserImg ={};
    
    var fb = new Firebase("https://bigzill.firebaseio.com/"); var fbAuth 
    = $firebaseAuth(fb); $scope.login    = function(email, password){
        if(email && password){
            console.log("valid Form");
            fbAuth.$authWithPassword({
                email: email,
                password: password
            }).then(function(authData){
                var authData = fb.getAuth();
                var usersRef2  = new Firebase("https://bigzill.firebaseio.com/users/"+authData.uid);
    
                usersRef2.on("value", function(snapshot) {
                  $scope.data = snapshot.val();
                  // data equals { "name": { "first": "Fred", "last": "Flintstone" }, "age": 53 }
                  //console.log($scope.data.username);  // "Fred"
                  //console.log($scope.data.image_name);  // 53
    
                });
                $state.go('tab.home');
            }).catch(function(error){
                console.log("Error: " + error);
                $ionicPopup.alert({
                    title: "Login Error",
                    template: error
                });
            });
        }else{
            console.log("Invalid Form");
        } } });
    

    这是我的观点代码:

    <ion-view view-title="Home Page">
    
        <ion-content class="has-tabs" style="margin-top:25%;">
            <!--search form-->
            <!-- <ion-sub-header>
                 <div class="bar bar-header item-input-inset">
                     <label class="item-input-wrapper">
                         <i class="icon ion-ios-search placeholder-icon"></i>
                         <input type="search" ng-model="query.search.name" placeholder="Search">
                     </label>
                 </div>
             </ion-sub-header>-->
            <!--end of search form-->
    
            <ul>
                <li ng-repeat="data in data">{{ data.username }}</li>
            </ul>
        </ion-content>
    
    </ion-view>
    

    请问我做错了什么

2 个答案:

答案 0 :(得分:1)

  
      
  1. 我有一个代码来登录firebase后显示我的信息,但它只是在控制台日志上推出,那不是我的意思   我希望在离子列表视图中显示数据。
  2.   

您只需要将fb.getAuth()的结果设置为范围变量,然后就可以像任何其他对象一样迭代它。它可以很简单:

$scope.authData = fb.getAuth();

然后在前端:

<p>{{authData.username}}</p>

看起来你正在尝试重复。但是getAuth只会返回当前用户的auth数据,所以没有什么可以重复的。如果要显示所有用户,则必须在Firebase中创建一个新子项以存储该数据,然后您可以迭代该对象。

  
      
  1. 我如何确保authData仍然存在于我的内部   范围,即使我刷新浏览器
  2.   

除非您更改会话长度或显式调用unauth(),否则您的身份验证数据会自动保留24小时。 Relevant doc

答案 1 :(得分:0)

哇.... 这个错误真的来自我。

  

我所做的是我在loginCtrl中调用我的$ scope.data   我的离子视图是从HomeCtrl调用的......

现在它的工作非常好,我感谢所有让我的代码铭记于心的人,我非常感谢您的关注。

相关问题