-
-
Save yusufola/a24a4cd25d35e22d93eaa88684716d08 to your computer and use it in GitHub Desktop.
Interceptor angular para Walladog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Intercept all requests /responses | |
* Ej. use to auth-tokens in headers | |
*/ | |
angular.module('cInterceptor', []) | |
.factory('cInterceptor', ['$q', '$rootScope', function ($q, $rootScope) { | |
return { | |
'request': function (config) { | |
config.headers = config.headers || {}; | |
//Get saved data of your custom header from sessionStorage | |
$rootScope.customHeader = sessionStorage.getItem(CUSTOM_HEADER); | |
config.headers = { | |
'Content-type': 'application/json;charset=UTF-8', | |
'Authorization': 'Bearer 8H04j1yP28FfMiStIl0E4jA8gGI62g' | |
}; | |
//Add custom header/data to request | |
config.headers[CUSTOM_HEADER] = $rootScope.customHeader; | |
return config; | |
}, | |
'response': function (response) { | |
//Save data custom header to send in next request | |
if (response.headers(CUSTOM_HEADER) !== null) { | |
$rootScope.customHeader = response.headers(CUSTOM_HEADER); | |
sessionStorage.setItem(CUSTOM_HEADER, response.headers(CUSTOM_HEADER)); | |
} else { | |
$rootScope.customHeader = sessionStorage.getItem(CUSTOM_HEADER); | |
} | |
response.headers('Allow', '*'); | |
return response; | |
}, | |
'responseError': function (rejection) { | |
var getUrlParam= function(parameterName) { | |
parameterName += "="; | |
var parameterValue = (location.hash.indexOf(parameterName)) ? location.hash.substring(location.hash.indexOf(parameterName) + parameterName.length) : null; | |
if (parameterValue !== null && parameterValue.indexOf('&') >= 0) { | |
parameterValue = parameterValue.substring(0, parameterValue.indexOf('&')); | |
} | |
return parameterValue; | |
}; | |
console.log('Reject::'); | |
var code = getUrlParam('code'); | |
console.log(code); | |
console.log(rejection); | |
return $q.reject(rejection); | |
} | |
}; | |
} | |
]); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment