Skip to content

Instantly share code, notes, and snippets.

@danjesus
Created March 15, 2016 20:19
Show Gist options
  • Save danjesus/7a8f0f72a66f9165958c to your computer and use it in GitHub Desktop.
Save danjesus/7a8f0f72a66f9165958c to your computer and use it in GitHub Desktop.
A simple angular interceptor
(function () {
'use strict';
angular.module('smartticket.services')
.factory('BearerAuthInterceptor', function ($window, $q, $localStorage, $location) {
return {
request: function (config) {
config.headers = config.headers || {};
var token = $localStorage.get('token');
if (token) {
config.headers.Authorization = 'Bearer ' + token;
}
return config || $q.when(config);
},
response: function (response) {
if (response.status === 401) {
$location.path('login');
}
return response || $q.when(response);
}
};
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment