Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Injecting auth headers with angularjs and restangular

I am creating a RESTFUL webservice that is called from an angularjs app which uses the restangular service. The REST service requires that an auth token is sent with each request. How can I get this sent with each request using restangular?

Looking at the documentation I can see there is a setRequestInterceptor method that I can set in the config which looks like I might be able to use:

RestangularProvider.setFullRequestInterceptor(function(element, operation, route, url, headers, params) {
      return {
        element: element,
        params: params,
        headers: _.extend(headers, {Authorization: 'Basic ' +  base64.encode('sometoken')})
      };
    });

I've not been able to get this to actually even get called by putting it in my config. I don't know where RestangularProvider provider needs to be injected from to get htis to work? I'd also want to create an external service which reads the token that has been generated and I'm not sure where to put this. Is there an example of this working anywhere? I've only been able to find scenarios that use $ngresource.

Any help appreciated.

like image 481
John Royal Avatar asked Nov 01 '22 14:11

John Royal


1 Answers

This was just an error on my own part. I was trying to chain this rather than include it as part of the config. A good example:

http://plnkr.co/edit/d6yDka?p=preview

like image 74
John Royal Avatar answered Nov 14 '22 23:11

John Royal