Advertisement
Guest User

Untitled

a guest
Jul 30th, 2015
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.37 KB | None | 0 0
  1. /* AuthService service definition */
  2. app.factory('AuthService', [
  3. '$q', '$location', '$toastr', '$cookies', '$sails',
  4. function ($q, $location, $toastr, $cookies, $sails) {
  5.  
  6. /*
  7. The cookie we use to store session information in
  8. */
  9. var authService, cookieName;
  10. cookieName = '_ideaSrc_authenticated_user';
  11.  
  12. /*
  13. The service object iself
  14. */
  15. authService = {
  16.  
  17. /*
  18. Used to do an initial setting
  19. */
  20. _initial: false,
  21.  
  22. /*
  23. Stores all the subscribers to the authentication user change event
  24. */
  25. subscribers: [],
  26.  
  27. /*
  28. Gets the current user session
  29. @force get from API directly
  30. */
  31. current: function (force) {
  32.  
  33. /* Telling whoever cares that we've done this at least once */
  34. this._initial = true;
  35.  
  36. /*
  37. If forced, we have to make an actual socket call, otherwise
  38. we'll use our trusty cookies to get the session
  39. */
  40. if (force != null) {
  41. return this.getSession();
  42. } else {
  43. return $cookies.get(cookieName);
  44. }
  45. },
  46.  
  47. /*
  48. Updates the current user session
  49. NB: Uses lodash debounce to spare this thing from being called non-stop
  50. */
  51. updateCurrent: _.debounce(function (current) {
  52.  
  53. /*
  54. If we've removed the session, remove the cookie
  55. Else update the cookie with the new session
  56. */
  57. var i, l;
  58. if (current == null) {
  59. $cookies.remove(cookieName);
  60. } else {
  61. $cookies.put(cookieName, current.user, {
  62. expires: current.expires
  63. });
  64. }
  65.  
  66. /* Notify everybody */
  67. i = 0;
  68. l = this.subscribers.length;
  69.  
  70. /* FYI function.apply() executes a function. */
  71. while (i < l) {
  72. this.subscribers[i++].apply();
  73. }
  74. return true;
  75. }, 250),
  76.  
  77. /*
  78. Subscribe to the authentication user change event
  79. */
  80. subscribe: function (cb) {
  81. return this.subscribers.push(cb);
  82. },
  83.  
  84. /*
  85. Gets the current session. Be careful of when and where you're calling
  86. this. It tends to go infinite loop a lot and I have no idea why.
  87. Probable cause is Angular's $digest cycle being a dick...
  88. */
  89. getSession: function () {
  90. return $sails.get('/users/session')
  91. .success((function (_this) {
  92. return function (session) {
  93.  
  94. /*
  95. Updating the current session.
  96. No errors -> session. Errors -> no session
  97. */
  98. _this.updatecurrent(session.error == null ? session : null);
  99.  
  100. /* Returning the current session value or nada */
  101. if (session.error == null) {
  102. return _this.current(false);
  103. } else {
  104. return null;
  105. }
  106. };
  107. })(this))
  108. .error(function () {
  109. return null;
  110. });
  111. },
  112.  
  113. /*
  114. Used to authenticate a request. Used as a "allowed" check mostly.
  115. @goToLogin if set to true will kick unauthorized people to the index page
  116. */
  117. authenticate: function (goToLogin) {
  118.  
  119. /* Directly getting the current user */
  120. $sails.get('/users/current')
  121. .success(function (response) {
  122.  
  123. /* We have a problem Houston */
  124. if (response.error && goToLogin) {
  125. $toastr('error', 'Not Authorized', 'You are not authorized to access this page.');
  126.  
  127. /* Go to the index page, pls */
  128. $location.path('/');
  129.  
  130. /* Denied. */
  131. return defer.reject('Not Authorized');
  132. } else {
  133.  
  134. /*
  135. Updating the current session.
  136. No errors -> session. Errors -> no session
  137. The reason we update the session here is to keep data consistency,
  138. maybe it updated since last time we checked.
  139. */
  140. this.updatecurrent(response.error == null ? response.session : null);
  141.  
  142. /* Tell whoever cares the good news */
  143. return defer.resolve(response);
  144. }
  145. })
  146. .error(function () {
  147.  
  148. /* Straight up deny requests on error */
  149. return defer.reject('Not authorized');
  150. });
  151. return defer.promise;
  152. }
  153. };
  154.  
  155. /* END OF LINE */
  156. return authService;
  157. }
  158. ]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement