Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. (function () {
  2. 'use strict';
  3.  
  4. angular
  5. .module('zigforumApp')
  6. .service('users', users);
  7.  
  8. users.$inject = ['$http', '$cookies', 'TOKEN_URL'];
  9.  
  10. function users($http, $cookies, TOKEN_URL) {
  11. var sv = this;
  12.  
  13. function NoAuthenticationException(message) {
  14. this.name = 'AuthenticationRequired';
  15. this.message = message;
  16. }
  17.  
  18. function AuthenticationExpiredException(message) {
  19. this.name = 'AuthenticationExpired';
  20. this.message = message;
  21. }
  22.  
  23. function AuthenticationRetrievalException(message) {
  24. this.name = 'AuthenticationRetrieval';
  25. this.message = message;
  26. }
  27.  
  28. sv.userData = {
  29. isAuthenticated: false,
  30. username: '',
  31. bearerToken: '',
  32. expirationDate: null
  33. };
  34.  
  35. function isAuthenticationExpired(expirationDate) {
  36. var now = new Date();
  37. expirationDate = new Date(expirationDate);
  38.  
  39. if (expirationDate - now > 0) {
  40. return false;
  41. } else {
  42. return true;
  43. }
  44. }
  45.  
  46. function saveData() {
  47. removeData();
  48. $cookies.putObject('auth_data', sv.userData);
  49. }
  50.  
  51. function removeData() {
  52. $cookies.remove('auth_data');
  53. }
  54.  
  55. function retrieveSavedData() {
  56. var savedData = $cookies.getObject('auth_data');
  57.  
  58. if (typeof savedData === 'undefined') {
  59. throw new AuthenticationRetrievalException('No authentication data exists');
  60. } else if (isAuthenticationExpired(savedData.expirationDate)) {
  61. throw new AuthenticationExpiredException('Authentication token has already expired');
  62. } else {
  63. sv.userData = savedData;
  64. setHttpAuthHeader();
  65. }
  66. }
  67.  
  68. function clearUserData() {
  69. sv.userData.isAuthenticated = false;
  70. sv.userData.username = '';
  71. sv.userData.bearerToken = '';
  72. sv.userData.expirationDate = null;
  73. }
  74.  
  75. function setHttpAuthHeader() {
  76. $http.defaults.headers.common.Authorization = 'Bearer ' + sv.userData.bearerToken;
  77. }
  78.  
  79. this.isAuthenticated = function () {
  80. if (!(sv.userData.isAuthenticated && !isAuthenticationExpired(sv.userData.expirationDate))) {
  81. try {
  82. retrieveSavedData();
  83. } catch (e) {
  84. return false;
  85. }
  86. }
  87.  
  88. return true;
  89. };
  90.  
  91. this.removeAuthentication = function () {
  92. removeData();
  93. clearUserData();
  94. $http.defaults.headers.common.Authorization = null;
  95. };
  96.  
  97. this.authenticate = function (username, password, persistData, successCallback, errorCallback) {
  98. this.removeAuthentication();
  99. var config = {
  100. method: 'POST',
  101. url: TOKEN_URL,
  102. headers: {
  103. 'Content-Type': 'application/x-www-form-urlencoded'
  104. },
  105. data: 'grant_type=password&username=' + username + '&password=' + password
  106. };
  107.  
  108. $http(config)
  109. .success(function (data) {
  110. sv.userData.isAuthenticated = true;
  111. sv.userData.username = data.userName;
  112. sv.userData.bearerToken = data.access_token;
  113. sv.userData.expirationDate = new Date(data['.expires']);
  114. setHttpAuthHeader();
  115. if (persistData === true) {
  116. saveData();
  117. }
  118. if (typeof successCallback === 'function') {
  119. successCallback();
  120. }
  121. })
  122. .error(function (data) {
  123. if (typeof errorCallback === 'function') {
  124. if (data && data.error_description) {
  125. errorCallback(data.error_description);
  126. } else {
  127. errorCallback('Unable to contact server; please, try again later.');
  128. }
  129. }
  130. });
  131. };
  132.  
  133. try {
  134. retrieveSavedData();
  135. } catch (e) { }
  136. }
  137. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement