Skip to content

Instantly share code, notes, and snippets.

@DaveDevTV
Created May 1, 2016 02:05
Show Gist options
  • Save DaveDevTV/bbfc83ef83c8562653ca1a692b68c6ea to your computer and use it in GitHub Desktop.
Save DaveDevTV/bbfc83ef83c8562653ca1a692b68c6ea to your computer and use it in GitHub Desktop.
How to Intercept HTTP requests from angular to include an authorization token
app.factory('TokenInterceptor', function($q, $cookies, $location, $rootScope, $injector) {
return {
request: function(config) {
config.headers = config.headers || {};
if($cookies.get('token')) {
config.headers.authorization = $cookies.get('token');
}
return config;
}
}
});
app.config(function($httpProvider) {
$httpProvider.interceptors.push('TokenInterceptor');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment