Skip to content

Instantly share code, notes, and snippets.

@auxcoder
Last active January 11, 2016 00:08
Show Gist options
  • Save auxcoder/36348216909efe66fd89 to your computer and use it in GitHub Desktop.
Save auxcoder/36348216909efe66fd89 to your computer and use it in GitHub Desktop.
httpInterceptorFactory to manage authentication token in header and redirection
(function() {
'use strict';
angular.module('engageAngularApp')
.config(httpInterceptor);
/** @ngInject */
function httpInterceptor($httpProvider) {
$httpProvider.interceptors.push('httpInterceptorFactory');
}
})();
(function() {
'use strict';
angular.module('engageAngularApp')
.factory('httpInterceptorFactory', httpInterceptorFactory);
/** @ngInject */
function httpInterceptorFactory($q, $rootScope, $timeout, localStorageService, $injector, $log) {
return {
// This method is called before $http sends the request to the backend, so you can modify the configurations and make other actions. This function receives the request configuration object as a parameter and has to return a configuration object or a promise. Returning an invalid configuration object or promise that will be rejected, will make the $http call to fail.
'request': function (config) {
// $rootScope.$broadcast('loading:show');
config.headers = config.headers || {};
var userToken = localStorageService.get('token');
// var currentState = $injector.get('$state').current.name;
if(userToken === null){
config.headers.Auth = '666';
} else {
config.headers.Auth = userToken;
}
// $log.log('token: ' + userToken);
config.headers.Accept = 'application/json, text/plain, */*';
return config;
},
// This method is called right after $http receives the response from the backend, so you can modify the response and make other actions. This function receives a response object as a parameter and has to return a response object or a promise. The response object includes the request configuration, headers, status and data that returned from the backend. Returning an invalid response object or promise that will be rejected, will make the $http call to fail.
'response': function(response) {
if(response.status === 401 || response.status === 403) {
$log.error(response.status);
$injector.get('$state').transitionTo('login');
}
// $rootScope.$broadcast('loading:hide');
if (response.data.hasOwnProperty('session_token')) {
// console.log(response.data.session_token);
localStorageService.set('token', response.data.session_token);
}
return response;
},
// Request error interceptor captures requests that have been canceled by a previous request interceptor. It can be used in order to recover the request and sometimes undo things that have been set up before a request, like removing overlays and loading indicators, enabling buttons and fields and so on.
// Sometimes our backend call fails. Other times it might be rejected by a request interceptor or by a previous response interceptor. In those cases, response error interceptor can help us to recover the backend call.
'responseError': function(response) {
if(response.status === 401 || response.status === 403 || response.status === 0) {
$log.error(response.status);
$injector.get('$state').transitionTo('login');
}
// console.log(response);
// $rootScope.$broadcast('loading:hide');
return $q.reject(response);
}
};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment