0

I'm trying to set the headers of a resource (code bellow). It happens that, when I instantiate my resource ($scope.user = new rsrUser;) angularjs fetches the cookies that aren't yet defined (an "undefined" error is fired from inside "getHMAC()"). The cookies will only be defined when "$scope.login()" is fired (it happens when the user clicks a button in the interface).

Is there a better way of doing this?

controllers.js

angularjsWebInterfaceControllers.controller('loginCtrl', ['$scope', 'rsrUser',
function($scope, rsrUser){
    $cookieStore.put("username","therebedragons");
    $cookieStore.put("password","therebedragons");

    $scope.user = new rsrUser;
    $scope.user.username = ""; //bound to input field in interface
    $scope.user.password = ""; //bound to input field in interface


    $scope.login = function() {
      $cookieStore.put("username", $scope.user.username);
      $cookieStore.put("password", $scope.user.password);
      $cookieStore.put("state", "loggedOUT");

      $scope.user.$logIn(
        function(){
           $cookieStore.put("state", "loggedIN");
        }, function() {
          $cookieStore.put("username","therebedragons");
          $cookieStore.put("password","therebedragons");
          $cookieStore.put("state", "loggedOUT");
        }
      )
     };
}]);

services.js

angularjsWebInterfaceServices.service('rsrUser', [ '$resource', '$cookieStore',
  function($resource, $cookieStore){
    var req = "/login"
    var timestamp = getMicrotime(true).toString();
    var username = $cookieStore.get("username");
    var key = $cookieStore.get("password");
    return $resource(baseURL + req, {}, {
      logIn: {method:'POST',
              isArray:false,
              headers:{
                  'X-MICROTIME': timestamp,
                  'X-USERNAME': username,
                  'X-HASH': getHMAC(username,timestamp,req,key)
                }
            }
    });
  }]);

EDIT: Actually, the cookies are defiend as soon as the controller is instantiated;

1 Answer 1

2

The value for a header can be a function that returns a string (see arguments here: http://docs.angularjs.org/api/ng/service/$http#usage). That way the cookie isn't accessed in your resource until the logIn method is called.

return $resource(baseURL + req, {}, {
  logIn: {method:'POST',
          isArray:false,
          headers: {
              'X-MICROTIME': timestamp,
              'X-USERNAME': function() {
                return $cookieStore.get("username");
              },
              'X-HASH': function() {
                var username = $cookieStore.get("username");
                return getHMAC(username,timestamp,req,key)
              }
            }
        }
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.