2015-01-12 26 views
17

我在Ionic中構建了一個應用程序,並開始深入瞭解Firebase身份驗證方法。到目前爲止,我已經設法通過Twitter設置登錄(我可以登錄和註銷)。如何在登錄時處理狀態(Ionic,Firebase,AngularJS)?

但是,如何設置離子框架的狀態,使得僅當登錄時顯示特定的狀態(以及頁面),以及登出時才顯示其他狀態?我到目前爲止的代碼如下所示。

理想的情況下我會像一個變量:

AuthRequired: true 

你如何做到這一點的,什麼是它叫什麼名字?

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', 'ngCordova', 'firebase', 'firebase.utils', 'starter.controllers', 'starter.services', 'starter.config', 'starter.auth']) 

.run(function($ionicPlatform, Auth, $rootScope) { 


    $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(); 
    } 
    }); 



    //stateChange event 
    $rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams){ 
     if (toState.authRequired && !Auth.isAuthenticated()){ //Assuming the AuthService holds authentication logic 
     // User isn’t authenticated 
     $state.transitionTo("login"); 
     event.preventDefault(); 
     } 
    }); 





}) 

.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', 
     authRequired: true 
     }, 
    } 
    }) 

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

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

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

    .state('tab.example', { 
    url: '/example', 
    views: { 
     'tab-example': { 
     templateUrl: 'templates/tab-example.html', 
     controller: 'ExampleCtrl', 
      authRequired: true 
     } 
    } 
    }) 


    .state('tab.overview', { 
    url: '/overview', 
    views: { 
     'tab-overview': { 
     templateUrl: 'templates/tab-overview.html', 
     controller: 'OverviewCtrl', 
      authRequired: true 
     } 
    } 
    }) 

    .state('tab.login', { 
    url: '/login', 
    views: { 
     'tab-login': { 
     templateUrl: 'templates/tab-login.html', 
     controller: 'LoginCtrl', 
      authRequired: true 
     } 
    } 
    }); 

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

}) 

的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 and simple login --> 
    <script src="https://cdn.firebase.com/libs/angularfire/0.9.1/angularfire.min.js"></script> 
    <script src="https://cdn.firebase.com/js/client/2.0.4/firebase.js"></script> 


    <!-- 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 animation="slide-left-right"></ion-nav-view> 



    </body> 
</html> 
+0

你使用的UI路由器或官方ngRouter?對於我自己,我使用ui路由器,並且在每種狀態下,我都可以保存自定義數據,如布爾名爲「public」的布爾值,它決定相應的狀態是否需要驗證。如果你想要類似的東西,我可以給你更詳細的信息。 –

+0

嗨Himmet。說實話,我不知道,我認爲是Ionic Framework的標準配置。是的,請分享你的方法! – AMG

+0

我能夠獲得接受的工作答案。我的代碼中的一個區別是,我將'authRequired'作爲狀態的一個屬性,而不是嵌套在狀態的視圖中。否則,在我看來,你需要修改'toState.authRequire'來檢查toState.view內部 – liampronan

回答

15

你幾乎沒有。您所需要的只是確保您的狀態使用自定義屬性「AuthRequired」進行標記,並監聽$ stateChangeStart事件以檢查身份驗證。每次在應用程序中移動時都會觸發此事件。

.run(function($ionicPlatform, AuthService) { 
     //ionic init code  

     //stateChange event 
     $rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams){ 
     if (toState.authRequired && !AuthService.isAuthenticated()){ //Assuming the AuthService holds authentication logic 
     // User isn’t authenticated 
     $state.transitionTo("login"); 
     event.preventDefault(); 
     } 
    }); 
} 

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

    .state('tab.chats', { 
     url: '/chats', 
     views: { 
     'tab-chats': { 
      templateUrl: 'templates/tab-chats.html', 
      controller: 'ChatsCtrl', 
      authRequired: true 
     } 
     } 
    }) 

擁有$ stateChangeStart事件處理程序的最佳場所是應用程序運行。

+0

感謝您的回答。我不明白在哪裏以及如何放置stateChangeStart? – AMG

+0

@AMG stateChange事件處理程序必須放置在初始化離子的運行函數中。運行函數是最接近角度的主要方法,它會在應用程序加載時執行。我更新了代碼 – Karthik

+0

感謝您的更新。它似乎沒有爲我工作,因爲我有.config中的我的狀態。 AuthService是Auth? – AMG

0

要使用成功讀取值:

toState.authRequired 

請將authRequired: true內的.state,而不是views

2

我有和你一樣的問題!看看我是如何解決它的!

app.js

angular.module('app', ['ionic','firebase', 'app.controllers', 'app.routes', 'app.directives','app.services','app.filters',]) 

.run(function($ionicPlatform, $rootScope, $state) { 
    $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 && window.cordova.plugins.Keyboard) { 
     cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true); 
     cordova.plugins.Keyboard.disableScroll(true); 
    } 
    if (window.StatusBar) {// 
     // org.apache.cordova.statusbar required 
     StatusBar.styleDefault(); 
    } 
    }); 

    //stateChange event 
    $rootScope.$on("$stateChangeStart", function(event, toState, toParams, fromState, fromParams){ 
    var user = firebase.auth().currentUser; 
    if (toState.authRequired && !user){ //Assuming the AuthService holds authentication logic 
     // User isn’t authenticated 
     $state.transitionTo("login"); 
     event.preventDefault(); 
    } 
    }); 
    // Initialize Firebase Here 

}) 

routes.js

angular.module('app.routes', ['ionicUIRouter']) 

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

    $stateProvider 

    .state('login', { 
    url: '/login', 
    templateUrl: 'templates/login.html', 
    controller: 'loginCtrl' 
    }) 

.state('menu', { 
    url: '/menu', 
    templateUrl: 'templates/menu.html', 
    abstract:true, 
    controller: 'menuCtrl' 
    }) 

    .state('menu.dash', { 
    url: '/contas', 
    templateUrl: 'templates/dash.html', 
    controller: 'contasCtrl', 
    authRequired: true 
    }) 

$urlRouterProvider.otherwise('/login') 

}); 
相關問題