Advertisement
Guest User

Untitled

a guest
Jun 14th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. angular.module('coop', ['ionic', 'coop.controllers', 'coop.services', 'satellizer'])
  2.  
  3. .constant('ApiEndpoint', {
  4. url: 'http://coop.app/api'
  5. })
  6.  
  7. .run(function($ionicPlatform, $rootScope, $auth, $state, $location) {
  8.  
  9. // Check for login status when changing page URL
  10. $rootScope.$on('$routeChangeStart', function (event, next) {
  11. var currentRoute = next.$$route;
  12.  
  13. if (!currentRoute || currentRoute.requiresAuth && !AuthenticationService.authenticated) {
  14. $location.path('/auth');
  15. }
  16. else if (!currentRoute || !currentRoute.requiresAuth && AuthenticationService.authenticated) {
  17. $location.path('/front');
  18. }
  19. });
  20.  
  21. $rootScope.logout = function() {
  22.  
  23. $auth.logout().then(function() {
  24.  
  25. // Remove the authenticated user from local storage
  26. localStorage.removeItem('user');
  27.  
  28. // Remove the current user info from rootscope
  29. $rootScope.currentUser = null;
  30. $state.go('main.auth');
  31. });
  32. }
  33.  
  34. $rootScope.token = localStorage.getItem('token');
  35.  
  36. $ionicPlatform.ready(function() {
  37. // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
  38. // for form inputs)
  39. if (window.cordova && window.cordova.plugins && window.cordova.plugins.Keyboard) {
  40. cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
  41. cordova.plugins.Keyboard.disableScroll(true);
  42.  
  43. }
  44. if (window.StatusBar) {
  45. // org.apache.cordova.statusbar required
  46. // StatusBar.styleDefault();
  47. StatusBar.show();
  48. StatusBar.overlaysWebView(false);
  49. StatusBar.styleLightContent();
  50. StatusBar.backgroundColorByHexString("#2a2e34");
  51. }
  52. });
  53. })
  54.  
  55. .config(function($stateProvider, $urlRouterProvider, $authProvider, ApiEndpoint) {
  56.  
  57. $authProvider.loginUrl = ApiEndpoint.url + '/authenticate';
  58.  
  59. $stateProvider
  60. .state('main', {
  61. url: '/main',
  62. abstract: true,
  63. templateUrl: 'templates/main.html',
  64. requiresAuth: true
  65. })
  66.  
  67. .state('main.auth', {
  68. url: '/auth',
  69. views: {
  70. 'content': {
  71. templateUrl: 'templates/login.html',
  72. controller: 'AuthController',
  73. requiresAuth: false
  74. }
  75. }
  76. })
  77.  
  78. .state('main.front', {
  79. url: '/front',
  80. views: {
  81. 'content': {
  82. templateUrl: 'templates/main-front.html',
  83. controller: 'FrontPageController',
  84. requiresAuth: true
  85. }
  86. }
  87. })
  88.  
  89. .state('main.article', {
  90. url: '/article/{id}',
  91. views: {
  92. 'content': {
  93. templateUrl: 'templates/main-article.html',
  94. controller: 'ArticleController',
  95. requiresAuth: true
  96. }
  97. }
  98. });
  99.  
  100. // if none of the above states are matched, use this as the fallback
  101. $urlRouterProvider.otherwise('/main/front');
  102. });
  103.  
  104. angular.module('coop.controllers', [])
  105.  
  106. .controller('FrontPageController', function($scope, ArticleService, $state) {
  107. ArticleService.all().then(function(data){
  108. $scope.articles = data;
  109. $scope.like = function(article){
  110. article.like = article.like == 0 ? 1 : 0;
  111. ArticleService.like(article.id, article.like)
  112. };
  113. })
  114. })
  115.  
  116. .controller('ArticleController', function($scope, ArticleService, $stateParams, $ionicSlideBoxDelegate, $auth) {
  117. ArticleService.get($stateParams.id).then(function(response) {
  118. $scope.article = response;
  119. $scope.commentsCount = response.comments.length;
  120. $scope.articleText = response.text;
  121.  
  122. $scope.like = function(){
  123. $scope.article.like = $scope.article.like == 0 ? 1 : 0;
  124. ArticleService.like($scope.article.id, $scope.article.like)
  125. };
  126.  
  127. $ionicSlideBoxDelegate.update();
  128. })
  129.  
  130. })
  131.  
  132. .controller('AuthController', function($scope, $location, $stateParams, $ionicHistory, $http, $state, $auth, $rootScope) {
  133. $scope.loginData = {}
  134. $scope.loginError = false;
  135. $scope.loginErrorText;
  136.  
  137. $scope.login = function() {
  138. var credentials = {
  139. email: $scope.loginData.email,
  140. password: $scope.loginData.password
  141. }
  142.  
  143. $auth.login(credentials).then(function(response) {
  144. var token = JSON.stringify();
  145. localStorage.setItem('token', response.data.token);
  146.  
  147. $ionicHistory.nextViewOptions({
  148. disableBack: true
  149. });
  150.  
  151. $state.go('main.front');
  152. }, function(){
  153. $scope.loginError = true;
  154. $scope.loginErrorText = error.data.error;
  155. });
  156. }
  157. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement