Advertisement
Guest User

Untitled

a guest
Nov 29th, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. .config(['$httpProvider', function ($httpProvider)
  2. {
  3. //interceptor for requests and error responses
  4. $httpProvider.interceptors.push(['$rootScope','$q', '$location', '$localStorage','Auth', function ($rootScope,$q, $location, $localStorage,Auth) {
  5. return {
  6. //injecting JWT token into header for requests
  7. 'request': function (config)
  8. {
  9. $rootScope.stateLoading = true;
  10. config.headers = config.headers || {};
  11. if ($localStorage.token) {
  12. config.headers.Authorization = 'Bearer ' + $localStorage.token;
  13. }
  14. return config;
  15. },
  16. //handling not authorized requests and sending them to the home page.
  17. 'responseError': function (response)
  18. {
  19.  
  20. $rootScope.stateLoading = false;
  21. if (response.status === 401 || response.status === 403 || response.status === 400)
  22. {
  23. $location.path('/home');
  24. }
  25. if(response.status === 498)
  26. {
  27. Auth.refresh($localStorage.token).then(function(response)
  28. {
  29. $localStorage.token = response.token;
  30. });
  31. }
  32. return $q.reject(response);
  33.  
  34. },
  35. 'response':function(response)
  36. {
  37. $rootScope.stateLoading = false;
  38. return response;
  39. }
  40. };
  41. }]);
  42. }])
  43. .config(['ngToastProvider', function(ngToastProvider)
  44. {
  45. ngToastProvider.configure({
  46. animation: 'slide',
  47. timeout:'5000'
  48. });
  49. }]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement