app.config(function ($httpProvider) {
    $httpProvider.interceptors.push('requestInterceptor');
});

app.factory('requestInterceptor', function ($q, $rootScope, $window, $location) {
    

    return {
        request: function (config) {
            //console.log(config);
            config.headers = config.headers || {};

                
                if ($window.sessionStorage.token && $location.path() != "/login") {

                    //config.headers.Authorization = 'Basic' + $window.sessionStorage.token;
                    config.headers.Authorization = '' + $window.sessionStorage.token;
                    config.headers.Atk = $window.sessionStorage.CibeAtk;
                }
                else if ($window.sessionStorage.CibeAtk && $location.path() != "/login") {
                    config.headers.Atk = $window.sessionStorage.CibeAtk;
                }
                else {

                    
                    $location.path('/login');

                }
                
                if (config.url.indexOf("template") == -1 && config.url.indexOf("angucomplete")==-1)
                    
                    config.url = config.url + '?cacheSlayer=' + new Date().getTime();
                
                return config || $q.when(config);
            
        },

        responseError: function(response) {
            console.log(response);
            if (response.status == 401) {
                $location.path('/login');
            }
            return $q.reject(response);
        },

        requestError: function (request) {
            $location.path('/login');
            return $q.reject(request);
        }


    }
});