Making an Angular $resource request work only after token definition

I'm trying to create a REST client using $resource and a request wrapper. You can see the code below. Everything works just fine, but I've a problem.

The RequestWrapper module is used to set the access token (from the fragment URI). What I need is to be able to block possible requests until the access token is set from the requestWrapper.set() function.

resources.factory('RequestWrapper', ['$http', '$q', function($http, $q) {
  var scope;
  var requestWrapper = {};
  var deferred = $q.defer();

  // get info about the token
  requestWrapper.get = function() { return scope; };

  // Set the info related to the token
  requestWrapper.set = function(newScope) {
    scope = newScope;
    $http.defaults.headers.common['Authorization'] = 'Bearer ' + scope.token.access_token;

    // Here I resolve the promise
    deferred.resolve(true);
  };

  requestWrapper.wrap = function(resource, actions) {
    var wrappedResource = resource;
    for (var i=0; i < actions.length; i++) { request(wrappedResource, actions[i]); };
    return wrappedResource;
  };

  var request = function(resource, action) {

    resource['_' + action]  = resource[action];

    resource[action] = function(param, data, success, error) {
      if (scope && scope.token.expires_at < new Date()) {
        window.location.replace(scope.endpoint)
      } else {
        return resource['_' + action](param, data, success, error);
      }
    };
  };

  return requestWrapper;
}]);

// Example on using the Request Wrapper
resources.factory('Profile', ['RequestWrapper', '$resource', function(RequestWrapper, $resource) {
  var resource = $resource(endpoint + '/me');
  return RequestWrapper.wrap(resource, ['get']);
}]);

I've tried to use promises (I'm not an expert) and I got the logic working behind it. I define it during the module initialization and I resolve it after the access token is defined. Now, my main concern is to understand where I can put the promise.then() method to let the request start only when the token is set.

deferred.promise.then(function() { ... })

I've tried to put it around resource['_' + action](param, data, success, error) in the wrapper function and some other places, but I feel like blind.

Thanks a lot for your time.

Why don't you use a Session service to provide the token, say in $scope.token and the trigger the subsequent actions with a $scope.$watch('token', ...) in the other controllers?

I recommend you to read the $http page in AngularJS documentation and if you still want to "block" the requests, you may use an interceptors (see http://code.angularjs.org/1.1.5/docs/api/ng.$http).