Created
April 1, 2014 20:57
-
-
Save kentcdodds/9923029 to your computer and use it in GitHub Desktop.
Example interceptor for jpotts
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
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