Advertisement
Guest User

Untitled

a guest
Jan 10th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.70 KB | None | 0 0
  1. angular.module('Spis', ['ui.router', 'ngRoute', 'LocalStorageModule', 'angular-loading-bar', 'oc.lazyLoad'])
  2.  
  3. .config(['$stateProvider', '$urlRouterProvider', '$ocLazyLoadProvider', function ($stateProvider, $urlRouterProvider, $httpProvider, $ocLazyLoadProvider) {
  4.  
  5. $urlRouterProvider.otherwise("/home");
  6.  
  7. $stateProvider
  8. .state("home", {
  9. url: '/home',
  10. templateUrl: '/app/views/home.html',
  11. controller: 'homeController',
  12. resolve: {
  13. deps: ['$ocLazyLoad', function ($ocLazyLoad) {
  14. return $ocLazyLoad.load({
  15. insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
  16. files: [
  17. 'app/controllers/homeController.js'
  18. ]
  19. });
  20. }]
  21. }
  22. })
  23. .state("login", {
  24. url: '/login',
  25. templateUrl: 'app/views/login.html',
  26. controller: 'loginController',
  27. resolve: {
  28. deps: ['$ocLazyLoad', function ($ocLazyLoad) {
  29. return $ocLazyLoad.load({
  30. insertBefore: '#ng_load_plugins_before', // load the above css files before a LINK element with this ID. Dynamic CSS files must be loaded between core and theme css files
  31. files: [
  32. 'app/controllers/loginController.js'
  33. ]
  34. });
  35. }]
  36. }
  37. })
  38. }])
  39.  
  40. .config(['$qProvider', function ($qProvider) {
  41. $qProvider.errorOnUnhandledRejections(false);
  42. }])
  43.  
  44. .config(['$httpProvider', function ($httpProvider) {
  45. $httpProvider.interceptors.push('authInterceptorService');
  46. }])
  47.  
  48. .run(["$rootScope", "$state", 'authService', function ($rootScope, $state, authService) {
  49. $rootScope.$state = $state; // state to be accessed from view
  50. authService.fillAuthData();
  51. }]);
  52.  
  53. 'use strict';
  54. angular.module('Spis').controller('loginController', ['$scope', '$location', '$state', 'authService', function ($scope, $location, $state, authService) {
  55.  
  56. $scope.loginData = {
  57. userName: "",
  58. password: ""
  59. };
  60.  
  61. $scope.message = "";
  62.  
  63. $scope.login = function (isValid) {
  64. if (isValid) {
  65. authService.login($scope.loginData).then(function (response) {
  66. console.log(authService.authentication);
  67. $state.go('school');
  68. },
  69. function (err) {
  70. $scope.message = err.error_description;
  71. });
  72. }
  73. };
  74.  
  75. }]);
  76.  
  77. 'use strict';
  78. angular.module('Spis').factory('authService', ['$http', '$q', 'localStorageService', function ($http, $q, localStorageService) {
  79.  
  80. var serviceBase = 'http://localhost:12258/';
  81. var authServiceFactory = {};
  82.  
  83. var _authentication = {
  84. isAuth: false,
  85. userName: ""
  86. };
  87.  
  88. var _login = function (loginData) {
  89.  
  90. var data = "grant_type=password&username=" + loginData.userName + "&password=" + loginData.password;
  91.  
  92. var deferred = $q.defer();
  93.  
  94. $http.post(serviceBase + 'token', data, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } })
  95. .then(function (response) {
  96. localStorageService.set('authorizationData', {
  97. token: response.data.access_token,
  98. userName: loginData.userName
  99. });
  100.  
  101. _authentication.isAuth = true;
  102. _authentication.userName = loginData.userName;
  103.  
  104. console.log(localStorageService.get('authorizationData'));
  105. deferred.resolve(response);
  106. })
  107.  
  108. .then(function (err, status) {
  109. _logOut();
  110. deferred.reject(err);
  111. });
  112.  
  113. return deferred.promise;
  114.  
  115. };
  116. var _fillAuthData = function () {
  117.  
  118. var authData = localStorageService.get('authorizationData');
  119. console.log(authData);
  120. if (authData) {
  121. _authentication.isAuth = true;
  122. _authentication.userName = authData.userName;
  123. }
  124. }
  125.  
  126. authServiceFactory.saveRegistration = _saveRegistration;
  127. authServiceFactory.login = _login;
  128. authServiceFactory.logOut = _logOut;
  129. authServiceFactory.fillAuthData = _fillAuthData;
  130. authServiceFactory.authentication = _authentication;
  131.  
  132. return authServiceFactory;
  133. }]);
  134.  
  135. 'use strict';
  136. angular.module('Spis').factory('authInterceptorService', ['$q', '$location', 'localStorageService', function ($q, $location, localStorageService) {
  137.  
  138. var authInterceptorServiceFactory = {};
  139.  
  140. var _request = function (config) {
  141.  
  142. config.headers = config.headers || {};
  143.  
  144. var lsKeys = localStorageService.keys();
  145. var authData = localStorageService.get('authorizationData');
  146. console.log(authData);
  147. if (authData) {
  148. config.headers.Authorization = 'Bearer ' + authData.token;
  149. }
  150.  
  151. return config;
  152. }
  153.  
  154. authInterceptorServiceFactory.request = _request;
  155.  
  156. return authInterceptorServiceFactory;
  157. }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement