angularjs - Limiting http interceptor to specific domain -


i'm busy in building little backoffice web client in angular.js directly talks api. authentication use oauth2 use http interceptor include access token request. but, interceptor global, not requests/responses api checked, other http call, static html , templates.

i bind interceptor api calls only.

would possible without nasty domain-string-comparing-if-statement in interceptor?

you can create service-wrapper around $http takes care of oauth specific stuff (e.g. adding header etc) , use service (instead of $http) oauth related requests.


sample implementation:

app.factory('httpoauth', function ($http) {     // augments request configuration object      // oauth specific stuff (e.g. header)     function getaugmentedconfig(cfg) {         var config  = cfg || {};         config.headers = config.headers || {};         config.headers.someheadername = 'some-header-value';         return config;     }      // service object returned (`$http` wrapper)     var serviceobj = {};      // create wrappers methods without data     ['delete', 'get', 'head', 'jsonp'].foreach(function (method) {         serviceobj[method] = function (url, config) {             var config = getaugmentedconfig(config);             return $http[method](url, config);         };     });      // create wrappers methods data     ['post', 'put'].foreach(function (method) {         serviceobj[method] = function (url, data, config) {             var config = getaugmentedconfig(config);             return $http[method](url, data, config);         };     });      // return service object     return serviceobj; }); 

see, also, short demo.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -