Created
January 18, 2016 02:56
-
-
Save diegopso/50eecb22a88e26d2a8e5 to your computer and use it in GitHub Desktop.
Angular Authorization with Intetceptors and ngStorage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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