Skip to content

Instantly share code, notes, and snippets.

@diegopso
Created January 18, 2016 02:56
Show Gist options
  • Save diegopso/50eecb22a88e26d2a8e5 to your computer and use it in GitHub Desktop.
Save diegopso/50eecb22a88e26d2a8e5 to your computer and use it in GitHub Desktop.
Angular Authorization with Intetceptors and ngStorage
$httpProvider.interceptors.push(['$q', '$location', '$localStorage', function($q, $location, $localStorage) {
return {
'request': function (config) {
config.headers = config.headers || {};
if ($localStorage.token) {
config.headers.Authorization = 'Portador ' + $localStorage.token;
}
return config;
},
'responseError': function(response) {
if(response.status === 401 || response.status === 403) {
$location.path('/signin');
}
return $q.reject(response);
}
};
}]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment