3

I have the following $resource definition:

angular.module("MyApp").factory("account", ["$resource",
function ($resource) {

    var userResource = $resource("http://localhost/api/account/:id",
        {
            id: "@id"
        },
        {
            register: {

                method: "Post",

            }
    );

    return userResource;
}]);

Before each call to user.query, get, register, etc... I need to set a custom header which is an authorization header with an access token.

Please note that setting the header globally would not work, because the access token might expire and be refreshed with a new one before every call to the $resource and therefore the authorization header will change. Also I need to set the header only on specific actions. For example I do not need it on the save action.

I read this article:

http://nils-blum-oeste.net/angularjs-send-auth-token-with-every-request/

which by the way is linked on several stackoverflow posts. But this is not what I need as it sets the token in the data of the action and I need it in the header.

Milen Kovachev
  • 5,111
  • 6
  • 41
  • 58

1 Answers1

0

for me, this was just a syntax problem - this post helped me out.

Dynamic Resource Headers

.factory("ResourceService", function() { 
   return { 
     Token: function(token){
        return $resource("someurl/checktoken", {}, {
           validateToken: {method:"POST", params: {}, headers: {"MY-AUTH-TOKEN": token}}
        });
 }}})
Community
  • 1
  • 1
110maor
  • 61
  • 1
  • 2
  • 4