Angularjs “Firebase认证”;粘的;在爱奥尼亚

Angularjs “Firebase认证”;粘的;在爱奥尼亚,angularjs,firebase,ionic-framework,angularfire,Angularjs,Firebase,Ionic Framework,Angularfire,我正在尝试将$firebaseAuth合并到我的爱奥尼亚项目中。我使用了一个示例,从Firebase使用Twitter(auth.$authWithOAuthPopup('Twitter'))登录,并将其合并到我的Ionic框架中。代码如下所示 我正在使用以下版本(截至今天,所有内容都是最新的): firebase 2.0.4 角火0.9.1 离子1.0.0-beta.14“镁猫鼬” 点击“登录”按钮可正确打开弹出窗口,我可以登录。然而,在此之后,在爱奥尼亚项目中,在实际登录时没有任何变化。当我

我正在尝试将
$firebaseAuth
合并到我的爱奥尼亚项目中。我使用了一个示例,从Firebase使用Twitter(
auth.$authWithOAuthPopup('Twitter')
)登录,并将其合并到我的Ionic框架中。代码如下所示

我正在使用以下版本(截至今天,所有内容都是最新的): firebase 2.0.4 角火0.9.1 离子1.0.0-beta.14“镁猫鼬”

点击“登录”按钮可正确打开弹出窗口,我可以登录。然而,在此之后,在爱奥尼亚项目中,在实际登录时没有任何变化。当我刷新浏览器时,它会显示我的displayname,并通知我已正确登录

当我尝试合并Firebase的ui路由器认证时,我有同样的“粘性”,也从网站复制代码。此外,我必须刷新以处理更改

为什么会有这样的延误?firebase不应该是实时的吗?我的猜测是,它必须与离子的设置有关,因为使用Angularfire种子(不带离子)效果非常好。或者是别的什么

app.js

// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.services' is found in services.js
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'firebase', 'starter.controllers', 'starter.services'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
  });
})

.config(function($stateProvider, $urlRouterProvider) {

  // Ionic uses AngularUI Router which uses the concept of states
  // Learn more here: https://github.com/angular-ui/ui-router
  // Set up the various states which the app can be in.
  // Each state's controller can be found in controllers.js
  $stateProvider

  // setup an abstract state for the tabs directive
    .state('tab', {
    url: "/tab",
    abstract: true,
    templateUrl: "templates/tabs.html"
  })

  // Each tab has its own nav history stack:

  .state('tab.dash', {
    url: '/dash',
    views: {
      'tab-dash': {
        templateUrl: 'templates/tab-dash.html',
        controller: 'DashCtrl'
      }
    }
  })

  .state('tab.chats', {
      url: '/chats',
      views: {
        'tab-chats': {
          templateUrl: 'templates/tab-chats.html',
          controller: 'ChatsCtrl'
        }
      }
    })
    .state('tab.chat-detail', {
      url: '/chats/:chatId',
      views: {
        'tab-chats': {
          templateUrl: 'templates/chat-detail.html',
          controller: 'ChatDetailCtrl'
        }
      }
    })

  .state('tab.friends', {
      url: '/friends',
      views: {
        'tab-friends': {
          templateUrl: 'templates/tab-friends.html',
          controller: 'FriendsCtrl'
        }
      }
    })
    .state('tab.friend-detail', {
      url: '/friend/:friendId',
      views: {
        'tab-friends': {
          templateUrl: 'templates/friend-detail.html',
          controller: 'FriendDetailCtrl'
        }
      }
    })

  .state('tab.account', {
    url: '/account',
    views: {
      'tab-account': {
        templateUrl: 'templates/tab-account.html',
        controller: 'AccountCtrl'
      }
    }
  });

  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/tab/dash');

});
angular.module('starter.services', [])

// let's create a re-usable factory that generates the $firebaseAuth instance
.factory("Auth", ["$firebaseAuth", function($firebaseAuth) {
  var ref = new Firebase("https://cloakit.firebaseio.com/");
  return $firebaseAuth(ref);
}])
// other controllers

// AccountCtrl
.controller("AccountCtrl", ["$scope", "Auth", function($scope, Auth) {
  $scope.settings = {
    enableFriends: true
  };

  $scope.auth = Auth;
  $scope.user = $scope.auth.$getAuth();
}]);
controllers.js

// Ionic Starter App

// angular.module is a global place for creating, registering and retrieving Angular modules
// 'starter' is the name of this angular module example (also set in a <body> attribute in index.html)
// the 2nd parameter is an array of 'requires'
// 'starter.services' is found in services.js
// 'starter.controllers' is found in controllers.js
angular.module('starter', ['ionic', 'firebase', 'starter.controllers', 'starter.services'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
    // for form inputs)
    if (window.cordova && window.cordova.plugins.Keyboard) {
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
    }
    if (window.StatusBar) {
      // org.apache.cordova.statusbar required
      StatusBar.styleDefault();
    }
  });
})

.config(function($stateProvider, $urlRouterProvider) {

  // Ionic uses AngularUI Router which uses the concept of states
  // Learn more here: https://github.com/angular-ui/ui-router
  // Set up the various states which the app can be in.
  // Each state's controller can be found in controllers.js
  $stateProvider

  // setup an abstract state for the tabs directive
    .state('tab', {
    url: "/tab",
    abstract: true,
    templateUrl: "templates/tabs.html"
  })

  // Each tab has its own nav history stack:

  .state('tab.dash', {
    url: '/dash',
    views: {
      'tab-dash': {
        templateUrl: 'templates/tab-dash.html',
        controller: 'DashCtrl'
      }
    }
  })

  .state('tab.chats', {
      url: '/chats',
      views: {
        'tab-chats': {
          templateUrl: 'templates/tab-chats.html',
          controller: 'ChatsCtrl'
        }
      }
    })
    .state('tab.chat-detail', {
      url: '/chats/:chatId',
      views: {
        'tab-chats': {
          templateUrl: 'templates/chat-detail.html',
          controller: 'ChatDetailCtrl'
        }
      }
    })

  .state('tab.friends', {
      url: '/friends',
      views: {
        'tab-friends': {
          templateUrl: 'templates/tab-friends.html',
          controller: 'FriendsCtrl'
        }
      }
    })
    .state('tab.friend-detail', {
      url: '/friend/:friendId',
      views: {
        'tab-friends': {
          templateUrl: 'templates/friend-detail.html',
          controller: 'FriendDetailCtrl'
        }
      }
    })

  .state('tab.account', {
    url: '/account',
    views: {
      'tab-account': {
        templateUrl: 'templates/tab-account.html',
        controller: 'AccountCtrl'
      }
    }
  });

  // if none of the above states are matched, use this as the fallback
  $urlRouterProvider.otherwise('/tab/dash');

});
angular.module('starter.services', [])

// let's create a re-usable factory that generates the $firebaseAuth instance
.factory("Auth", ["$firebaseAuth", function($firebaseAuth) {
  var ref = new Firebase("https://cloakit.firebaseio.com/");
  return $firebaseAuth(ref);
}])
// other controllers

// AccountCtrl
.controller("AccountCtrl", ["$scope", "Auth", function($scope, Auth) {
  $scope.settings = {
    enableFriends: true
  };

  $scope.auth = Auth;
  $scope.user = $scope.auth.$getAuth();
}]);
选项卡account.html

<ion-view view-title="Account">
  <ion-content>
    <ion-list>
      <ion-item class="item-toggle">
        Enable Friends
        <label class="toggle">
          <input type="checkbox" ng-model="settings.enableFriends">
          <div class="track">
            <div class="handle"></div>
          </div>
        </label>
      </ion-item>
      <ion-item>
        <div ng-show="user">
      <p>Hello, {{ user.twitter.displayName }}</p>
      <button ng-click="auth.$unauth()">Logout</button>
    </div>
    <div ng-hide="user">
      <p>Welcome, please log in.</p>
      <button ng-click="auth.$authWithOAuthPopup('twitter')">Login</button>
    </div>
      </ion-item>
    </ion-list>
  </ion-content>
</ion-view>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->



    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>

        <!-- firebase js -->
    <script src="lib/firebase/firebase.js"></script>
    <script src="lib/firebase/angularfire/angularfire.min.js"></script>

    <!-- loading bar -->
    <script src="lib/angular-loading-bar/src/loading-bar.js"></script>
    <link href='lib/angular-loading-bar/src/loading-bar.css' rel='stylesheet' />


    <!-- cordova script (this will be a 404 during development) -->
    <script src="lib/ngCordova/dist/ng-cordova.js"></script>
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>
    <script src="js/services.js"></script>
  </head>
  <body ng-app="starter">
    <!--
      The nav bar that will be updated as we navigate between views.
    -->
    <ion-nav-bar class="bar-stable">
      <ion-nav-back-button>
      </ion-nav-back-button>
    </ion-nav-bar>
    <!--
      The views will be rendered in the <ion-nav-view> directive below
      Templates are in the /templates folder (but you could also
      have templates inline in this html file if you'd like).
    -->
    <ion-nav-view></ion-nav-view>
  </body>
</html>

启用朋友
您好,{user.twitter.displayName}

注销 欢迎光临,请登录

登录
index.html

<ion-view view-title="Account">
  <ion-content>
    <ion-list>
      <ion-item class="item-toggle">
        Enable Friends
        <label class="toggle">
          <input type="checkbox" ng-model="settings.enableFriends">
          <div class="track">
            <div class="handle"></div>
          </div>
        </label>
      </ion-item>
      <ion-item>
        <div ng-show="user">
      <p>Hello, {{ user.twitter.displayName }}</p>
      <button ng-click="auth.$unauth()">Logout</button>
    </div>
    <div ng-hide="user">
      <p>Welcome, please log in.</p>
      <button ng-click="auth.$authWithOAuthPopup('twitter')">Login</button>
    </div>
      </ion-item>
    </ion-list>
  </ion-content>
</ion-view>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->



    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>

        <!-- firebase js -->
    <script src="lib/firebase/firebase.js"></script>
    <script src="lib/firebase/angularfire/angularfire.min.js"></script>

    <!-- loading bar -->
    <script src="lib/angular-loading-bar/src/loading-bar.js"></script>
    <link href='lib/angular-loading-bar/src/loading-bar.css' rel='stylesheet' />


    <!-- cordova script (this will be a 404 during development) -->
    <script src="lib/ngCordova/dist/ng-cordova.js"></script>
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>
    <script src="js/services.js"></script>
  </head>
  <body ng-app="starter">
    <!--
      The nav bar that will be updated as we navigate between views.
    -->
    <ion-nav-bar class="bar-stable">
      <ion-nav-back-button>
      </ion-nav-back-button>
    </ion-nav-bar>
    <!--
      The views will be rendered in the <ion-nav-view> directive below
      Templates are in the /templates folder (but you could also
      have templates inline in this html file if you'd like).
    -->
    <ion-nav-view></ion-nav-view>
  </body>
</html>

您可能需要

$onAuth侦听对客户端身份验证状态的更改

因此,您的代码如下所示:

$scope.auth = Auth;

var authData = $scope.auth.$getAuth();

$scope.auth.$onAuth(function(authData) {
    if (authData) {
        $scope.user = authData
    } else {
        console.error("Could not retrieve user");
    }
});
您可能需要运行
$scope.$apply()
。我想你不会的,因为$firebase应该在Angulars digest周期内,但如果它仍然不起作用,我会在那里放一个,以确保你不需要它

$onAuth侦听对客户端身份验证状态的更改

因此,您的代码如下所示:

$scope.auth = Auth;

var authData = $scope.auth.$getAuth();

$scope.auth.$onAuth(function(authData) {
    if (authData) {
        $scope.user = authData
    } else {
        console.error("Could not retrieve user");
    }
});
您可能需要运行
$scope.$apply()
。我想你不会的,因为$firebase应该在Angulars digest周期内,但如果它仍然不起作用,我会在那里放一个,以确保你不需要它

$onAuth侦听对客户端身份验证状态的更改

因此,您的代码如下所示:

$scope.auth = Auth;

var authData = $scope.auth.$getAuth();

$scope.auth.$onAuth(function(authData) {
    if (authData) {
        $scope.user = authData
    } else {
        console.error("Could not retrieve user");
    }
});
您可能需要运行
$scope.$apply()
。我想你不会的,因为$firebase应该在Angulars digest周期内,但如果它仍然不起作用,我会在那里放一个,以确保你不需要它

$onAuth侦听对客户端身份验证状态的更改

因此,您的代码如下所示:

$scope.auth = Auth;

var authData = $scope.auth.$getAuth();

$scope.auth.$onAuth(function(authData) {
    if (authData) {
        $scope.user = authData
    } else {
        console.error("Could not retrieve user");
    }
});
您可能需要运行
$scope.$apply()
。我想你不会的,因为$firebase应该在Angulars digest周期内,但如果它仍然不起作用,我会在那里放一个,以确保你不需要它