【问题标题】:Firebase Authentication "sticky" in IonicIonic 中的 Firebase 身份验证“粘性”
【发布时间】:2015-01-15 10:55:41
【问题描述】:

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

我正在使用以下版本(截至今天,一切都是最新的): 火力基地 2.0.4 角火 0.9.1 ionic 1.0.0-beta.14 "镁-猫鼬"

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

在尝试合并 Firebase 的 ui-router 身份验证时,我也遇到了同样的“粘性”,同时还从网站上复制了代码。此外,我必须刷新才能处理更改。

为什么会出现这种延迟? firebase 不应该是实时的吗?我的猜测是它必须与 Ionic 的设置有关,因为使用 Angularfire-seed(没有 Ionic)工作得非常好。或者可能是别的什么?

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');

});

services.js

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);
}])

controllers.js

// other controllers

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

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

tab-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>

index.html

<!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>

【问题讨论】:

    标签: angularjs firebase ionic-framework angularfire


    【解决方案1】:

    您可能想take a look at $onAuth

    $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 摘要周期内,但如果它仍然不起作用,我会在那里扔一个,以确保你不需要它

    【讨论】:

      猜你喜欢
      • 2019-02-21
      • 2017-08-13
      • 2018-06-09
      • 2016-10-05
      • 2020-04-25
      • 2016-07-09
      • 2018-09-29
      • 2016-12-08
      • 2017-06-16
      相关资源
      最近更新 更多