0

I am currently trying to make a Token-Based auth between a NodeJS server and AngularJS client.

In other words: Server generates token, sends to client in http header, client receives and sets that token to be sent in the next request (in the http header), when the request gets to the server it's checked if it contains a previously sent token. Repeat.

The problem is I'm not able to read the headers from AngularJS, even though I can see the header was successfully set in the response, through Chrome's console.

So far, I've got an AngularJS interceptor (What I've been told I should do) like this:

angular
    .module('injector', [])
    .factory('sessionInjector', function(){

      var injectedSession = {
          response: function(res) {

                console.log("RESPONSE: " + JSON.stringify(res.config.headers));

              return res;
          };

      return injectedSession;
});

And this is the output:

RESPONSE: {"Accept":"application/json, text/plain, */*"}

I've just started with AngularJS and I'm a little lost. Hope I didn't mess up something really basic.

Thank you.

2
  • Are you sure that you are printing the right response? Commented Nov 16, 2016 at 10:15
  • I am, I answered my own question below, after hours of research I found the answer on another stackoverflow thread. Commented Nov 17, 2016 at 10:57

2 Answers 2

2

When you hit a http request it looks something like below :-

$http.get("your URL",)
      .success(function (data,status, headers, config) {
        console.log(headers('Content-Encoding'));
      })
      .error(function (error) {
          canceller.reject(error);
      });
}

And you will receive response from server with other detail in callback, and you can headers like above.

I hope it will help you. Thanks

Sign up to request clarification or add additional context in comments.

4 Comments

Isn't that to make a one and only request? Or does that allow me to intercept every response that gets to the client?
you can make a global service for request and use that service every time you request a server.
like module.factory("deferredService", ['$http', function($http){ var service = {}; service.get = function (url) { $http.get(url) .success(function (data,status, headers, config) { console.log(headers('Content-Encoding')); }) .error(function (error) { canceller.reject(error); }); } return service; }]); Include this service in your Angular controller and call like deferredService.get("your URL") .then(function (response) { });
Thank you for your help, thanks to you I was able to validate that the headers were arriving properly to the server. I'm going to try to implement what you've just told me to see if it fixes the remaining errors.
0

It seems that changing

response

for

'response'

made the trick.

I don't know why that did happen.

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.