Advertisement
Guest User

Untitled

a guest
Nov 15th, 2016
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. // controllers.js
  2. .controller('AuthCtrl', function($scope, $location, $stateParams, $ionicHistory, $http, $state, $auth, $rootScope) {
  3.  
  4. $scope.loginData = {};
  5. $scope.loginError = false;
  6. $scope.loginErrorText;
  7.  
  8. $scope.doLogin = function() {
  9.  
  10. var credentials = {
  11. username: $scope.loginData.username,
  12. password: $scope.loginData.password
  13. }
  14. var header = {
  15. 'Content-Type' : 'application/json',
  16. 'Accept' : 'application/json'
  17. }
  18.  
  19. $auth.login(credentials).then(function() {
  20.  
  21. $http.post('http://fullday-backend/api/users/token', header).success(function(response) {
  22.  
  23. var user = JSON.stringify(response.user);
  24.  
  25. localStorage.setItem('user', user);
  26.  
  27. $rootScope.currentUser = response.user;
  28.  
  29. $ionicHistory.nextViewOptions({
  30. disableBack: true
  31. });
  32.  
  33. $state.go('app.coupons');
  34. }).error(function() {
  35. $scope.loginError = true;
  36. $scope.loginErrorText = error.data.error;
  37. console.log($scope.loginErrorText);
  38. console.log("error");
  39. })
  40. });
  41. }
  42. })
  43.  
  44. // App.js
  45.  
  46. .config(function($stateProvider, $urlRouterProvider, $authProvider) {
  47.  
  48. $authProvider.loginUrl = 'http://fullday-backend/api/users/token';
  49.  
  50. $stateProvider
  51.  
  52. .state('app.auth', {
  53. url: '/auth',
  54. views: {
  55. 'menuContent': {
  56. templateUrl: 'templates/login.html',
  57. controller: 'AuthCtrl'
  58. }
  59. }
  60. })
  61.  
  62. .state('app', {
  63. url: '/app',
  64. abstract: true,
  65. templateUrl: 'templates/menu.html',
  66. controller: 'AppCtrl'
  67. })
  68.  
  69. .state('app.search', {
  70. url: '/search',
  71. views: {
  72. 'menuContent': {
  73. templateUrl: 'templates/search.html'
  74. }
  75. }
  76. })
  77.  
  78. .state('app.browse', {
  79. url: '/browse',
  80. views: {
  81. 'menuContent': {
  82. templateUrl: 'templates/browse.html'
  83. }
  84. }
  85. })
  86. .state('app.coupons', {
  87. url: '/coupons',
  88. views: {
  89. 'menuContent': {
  90. templateUrl: 'templates/coupons.html',
  91. controller: 'couponsCtrl'
  92. }
  93. }
  94. })
  95.  
  96. .state('app.single', {
  97. url: '/coupons/:couponId',
  98. views: {
  99. 'menuContent': {
  100. templateUrl: 'templates/coupon.html',
  101. controller: 'couponCtrl'
  102. }
  103. }
  104. });
  105. // if none of the above states are matched, use this as the fallback
  106. $urlRouterProvider.otherwise('/app/coupons');
  107. });
  108.  
  109. $http({
  110. method: 'POST',
  111. url: 'http://fullday-backend/api/users/token',
  112. dataType: "json",
  113. headers = {
  114. 'Content-Type' : 'application/json',
  115. 'Accept' : 'application/json'
  116. }
  117.  
  118. }).then(function(response) {
  119.  
  120. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement