Skip to content

Instantly share code, notes, and snippets.

@kentcdodds
Created April 1, 2014 20:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kentcdodds/9923029 to your computer and use it in GitHub Desktop.
Save kentcdodds/9923029 to your computer and use it in GitHub Desktop.
Example interceptor for jpotts
angular.module('bs.common.models').factory('AuthInterceptor', function ($rootScope, $q, $window) {
return {
request: function (config) {
config.headers = config.headers || {};
var token = $window.localStorage.getItem('user-token');
if (token) {
config.headers.Authorization = 'Bearer ' + token;
}
return config;
},
response: function (response) {
if (response.status === 401) {
console.warn('user not authenticated', response);
// handle the case where the user is not authenticated
}
return response || $q.when(response);
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment