1

Im building a RESTFul app and using Angular for my view. I want to use resources since is the best approach to it, i follow the how-to's and made some tweaks by my own to include the header api token, the code end like this:

fcbMixApp.factory('Resources', ['$resource',
    function ($resource) {
        return {
            seminary: function (apiToken) {
                return $resource('api/seminaries/:seminary', {}, {
                    save: {
                        method: 'POST',
                        headers: {
                            'Authorization': 'Bearer ' + apiToken
                        }
                    },
                    update: {
                        method: 'PUT',
                        headers: {
                            'Authorization': 'Bearer ' + apiToken
                        }
                    }
                });
            },
            attendant: function (apiToken) {
                return $resource('api/attendants/:attendant', {}, {
                    save: {
                        method: 'POST',
                        headers: {
                            'Authorization': 'Bearer ' + apiToken
                        }
                    },
                    update: {
                        method: 'PUT',
                        headers: {
                            'Authorization': 'Bearer ' + apiToken
                        }
                    }
                });
            }
        }
    }]);

But when i call it on my controller like this:

var Seminary = Resources.seminary(User.getAuthData().access_token);

I dont expect that line to make any request to my api, but it does. My code follows:

Seminary.query(function (data) {
                $scope.seminaries = data;
            });

So i finally make two calls.

What im doing wrong, or what should i change.

Thanks in advance.

bitgandtter
  • 2,179
  • 6
  • 31
  • 60

2 Answers2

1

You should set a header with the token:

    $http.defaults.headers.common["Authorization"] = 'Bearer' + apiToken;

And not in the resource itself. You should set this when the user is logged in the first time, then you will send it on all requests.

Also consider your resource looking something like this, and making a separate one for attendant:

   fcbMixApp.factory('Resources', ['$resource', function ($resource) {
        function setRequestData(data) {
            var requestData = new Object();
            requestData.seminary = data;
            return angular.toJson(requestData);
        }
        return $resource('api/seminaries/:seminary', {}, {
            save: {
                method: 'POST',
                headers: {"Content-Type": "application/json"},
                transformRequest: setRequestData
            },
            update: {
                method: 'PUT',
                headers: {"Content-Type": "application/json"},
                transformRequest: setRequestData
            }
        });
    }]);
Lucas
  • 9,871
  • 5
  • 42
  • 52
  • Thanks for the line to set the headers with auth afther login, but my main issue its that for some reason the query its executing twice and hitting my api server twice – bitgandtter Sep 26 '15 at 23:49
0

Here is the solution for adding your Resource Authorization headers.

AngularJS: How to send auth token with $resource requests?

Community
  • 1
  • 1
Rubel hasan
  • 2,522
  • 1
  • 13
  • 22