(function() { 'use strict'; angular .module('app') .run([ '$rootScope', '$state', '$stateParams', 'sessionService', 'app.SETTINGS', "storageManager", function($rootScope, $state, $stateParams, sessionService, APP_SETTINGS, storageManager) { // GLOBAL APP SCOPE // set below basic information $rootScope.$state = $state; $rootScope.$stateParams = $stateParams; // Assign a value to a variable, which contains common application information. $rootScope.app = APP_SETTINGS.APP_INFO; // Assign a value to a variable, which contains common application information. $rootScope.configuration = APP_SETTINGS; // Declare storage list to allow perform CRUD operations throughout client-side. window.appLocalStorage = storageManager.specify("localDB"); window.appCookieStorage = storageManager.specify("cookie"); }]) .config(['cfpLoadingBarProvider', function(cfpLoadingBarProvider) { cfpLoadingBarProvider.includeBar = true; cfpLoadingBarProvider.includeSpinner = false; } ]) .config(['$translateProvider', function($translateProvider) { $translateProvider.useStaticFilesLoader({ prefix: 'i18n/', suffix: '.json' }); $translateProvider.preferredLanguage('en'); } ]) .config(function($httpProvider) { $httpProvider.interceptors.push('authInterceptor'); }) .factory('authInterceptor', authInterceptor); function authInterceptor($rootScope, $q, $window, uiNotifications, appSettings, authorizeHelper) { function reloadPageUnauthorized(){ appSettings.RELOAD_PAGE_UNAUTHORIZED && $rootScope.$state.reload(); } return { request: function(config) { config.headers = config.headers || {}; var token = appLocalStorage.get("token"); if (token) { config.headers.Authorization = 'Bearer ' + token; $rootScope.isAuthenticated = true; } else { $rootScope.isAuthenticated = false; } return config; }, response: function(response, status, headers) { if (response.status === 401) { appSettings.DEVELOPMENT && console.error(uiNotifications.msg.Unauthorized, "warning"); authorizeHelper.clearUserRules(); $rootScope.$state.go('login'); } return response || $q.when(response); }, responseError: function(response){ if (response.status == 401) { appSettings.DEVELOPMENT && console.error(uiNotifications.msg.Unauthorized, "warning"); authorizeHelper.clearUserRules(); $rootScope.$state.go('login'); } else { appSettings.DEVELOPMENT && console.error(uiNotifications.code[response.status]); } return response || $q.when(response); } }; } // IoC container. authInterceptor.$inject = [ "$rootScope", "$q", "$window", "appUtils.uiNotifications", "app.SETTINGS", "helpers.authorize" ]; })();