Skip to content

Instantly share code, notes, and snippets.

@anderson-mota
Forked from danjesus/AuthInterceptor.js
Created March 15, 2016 22:58
Show Gist options
  • Save anderson-mota/8603c9f4d5454e35cde3 to your computer and use it in GitHub Desktop.
Save anderson-mota/8603c9f4d5454e35cde3 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