Skip to content

Instantly share code, notes, and snippets.

@ansariabr
Last active February 29, 2016 13:13
Show Gist options
  • Save ansariabr/76bf8411243462486872 to your computer and use it in GitHub Desktop.
Save ansariabr/76bf8411243462486872 to your computer and use it in GitHub Desktop.
Interceptor for token based authentication
app.factory('authInterceptor', ['$q', function ($q) {
return {
request: function (config) {
config.headers = config.headers || {};
if (config.headers.skipAuthorization === false) {
var token = localStorage.getItem('authenticationToken');
if (token != null) {
config.headers.Authorization = token;
}
}
return config;
},
response: function (response) {
if (response.headers("Authorization") != undefined || response.headers("Authorization") != '') {
localStorage.setItem('authenticationToken', response.headers("Authorization"));
}
return response;
},
responseError: function (rejection) {
if (rejection.status === "401") {
localStorage.removeItem('authenticationToken');
}
return $q.reject(rejection);
}
};
} ]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment