登录时如何处理状态(Ionic,Firebase,AngularJS)?

前端之家收集整理的这篇文章主要介绍了登录时如何处理状态(Ionic,Firebase,AngularJS)?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在建立一个应用程序在Ionic,并开始挖掘Firebase身份验证方法。到目前为止,我已经设法通过Twitter设置登录(我可以登录和注销)。

但是,如何设置离子框架的状态,从而只有当登录时才会显示特定的状态(因此页面)以及其他登录时的状态?我到目前为止的代码如下所示。

理想情况下,我会有一个像变量的东西:

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>
你几乎在那里所有你需要的是确保你的状态被标记为适当的自定义属性“Authrequired”并监听$ stateChangeStart事件来检查身份验证。每次在应用程序中移动时,都会触发此事件。
.run(function($ionicPlatform,AuthService) {
      //ionic init code    

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

.state('tab.dash',authrequired: true
      }
    }
  })

  .state('tab.chats',authrequired: true
        }
      }
    })

$ stateChangeStart事件处理程序的最佳位置将是应用程序运行。

原文链接:https://www.f2er.com/angularjs/144283.html

猜你在找的Angularjs相关文章