PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/client/app/app.js

https://gitlab.com/ewhalan/mining-webui
JavaScript | 54 lines | 45 code | 5 blank | 4 comment | 6 complexity | 3a7b6b0127150681e20cb56feeab42c3 MD5 | raw file
  1. 'use strict';
  2. angular.module('miningWebuiApp', [
  3. 'ngCookies',
  4. 'ngResource',
  5. 'ngSanitize',
  6. 'btford.socket-io',
  7. 'ui.router',
  8. 'ui.bootstrap'
  9. ])
  10. .config(function ($stateProvider, $urlRouterProvider, $locationProvider, $httpProvider) {
  11. $urlRouterProvider
  12. .otherwise('/');
  13. $locationProvider.html5Mode(true);
  14. $httpProvider.interceptors.push('authInterceptor');
  15. })
  16. .factory('authInterceptor', function ($rootScope, $q, $cookieStore, $location) {
  17. return {
  18. // Add authorization token to headers
  19. request: function (config) {
  20. config.headers = config.headers || {};
  21. if ($cookieStore.get('token')) {
  22. config.headers.Authorization = 'Bearer ' + $cookieStore.get('token');
  23. }
  24. return config;
  25. },
  26. // Intercept 401s and redirect you to login
  27. responseError: function(response) {
  28. if(response.status === 401) {
  29. $location.path('/login');
  30. // remove any stale tokens
  31. $cookieStore.remove('token');
  32. return $q.reject(response);
  33. }
  34. else {
  35. return $q.reject(response);
  36. }
  37. }
  38. };
  39. })
  40. .run(function ($rootScope, $location, Auth) {
  41. // Redirect to login if route requires auth and you're not logged in
  42. $rootScope.$on('$stateChangeStart', function (event, next) {
  43. Auth.isLoggedInAsync(function(loggedIn) {
  44. if (next.authenticate && !loggedIn) {
  45. $location.path('/login');
  46. }
  47. });
  48. });
  49. });