Skip to content

Instantly share code, notes, and snippets.

@jpotts18
Forked from kentcdodds/AuthInterceptor.js
Created April 1, 2014 23:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jpotts18/9925238 to your computer and use it in GitHub Desktop.
Save jpotts18/9925238 to your computer and use it in GitHub Desktop.
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