Skip to content

Instantly share code, notes, and snippets.

@leosilvadev
Created March 29, 2016 01:59
Show Gist options
  • Save leosilvadev/4fa5d6e4c5653d62669f to your computer and use it in GitHub Desktop.
Save leosilvadev/4fa5d6e4c5653d62669f to your computer and use it in GitHub Desktop.
angular.module('blogjs.autorizacao', ['blogjs.interceptadores']).config(function($httpProvider){
$httpProvider.interceptors.push('AutorizadorInterceptor');
});
angular.module('blogjs.interceptadores').factory('AutorizadorInterceptor', function interceptor($location) {
return {
request: function (config) {
var usuarioLogado = localStorage.getItem('usuarioLogado');
if ( usuarioLogado ) {
config.headers['Authorization'] = 'Basic ' + btoa(usuarioLogado.login+':'usuarioLogado.senha);
}
return config;
},
responseError: function(response) {
if (response.status === 401) {
$location.path('/login');
}
}
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment