Skip to content

Instantly share code, notes, and snippets.

@caferrari
Created November 19, 2015 15:58
Show Gist options
  • Save caferrari/c6df6c576ecec727146e to your computer and use it in GitHub Desktop.
Save caferrari/c6df6c576ecec727146e to your computer and use it in GitHub Desktop.
(function() {
'use strict';
angular.module('App')
.factory('authInterceptor', ['API', 'auth', authInterceptor]);
function authInterceptor(API, auth) {
return {
request: function(config) {
if (config.url.indexOf(API) === 0) {
config.headers.Authorization = 'Bearer ' + auth.getToken();
config.headers.Accept = 'application/json';
}
return config;
},
response: function(res) {
var token = res.headers('X-Token');
if (token) {
auth.setToken(token);
}
return res;
},
};
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment