witoldsz / angular-http-auth

MIT License
2.38k stars 417 forks source link

Pass a function to setDefaultHeaders instead of an object #35

Closed scollinson closed 11 years ago

scollinson commented 11 years ago

I'm using angular-http-auth with an API that uses a token in a header for authentication. I am capturing 401 responses and prompting the user to create a token. Once this token is created I would then like to add it to the headers of any further requests and also the request that has been deferred.

Passing a function to setDefaultHeaders would allow me to use $cookieStore to fetch the token for further requests. Adding the header to the deferred request I have no idea how to do.

scollinson commented 11 years ago

Sorry, as you may have been able to tell, this is tied to using restangular with angular-http-auth. What is happening is that I am able to set the header for future requests but the replayed request uses the old config and hence the old headers.

scollinson commented 11 years ago

See my pull request.

utilityboy commented 10 years ago

Hi @scollinson, would you mind sharing a gist of how you're doing your token setting wrt restangular headers? I've been struggling with this for a few hours and have something working but I feel I'm going about things the wrong way. Many thanks.

dv336699 commented 10 years ago

I'm using the following to store my CSRF token: $http.defaults.headers.post['x-csrf-token'] = response['X-Csrf-Token']; Maybe this helps?

utilityboy commented 10 years ago

Thanks @diego-vieira . What I'm actually asking is how to integrate angular-http-auth with restangular as @scollinson described above to set headers for the deferred requests so that when they're replayed they include the token received via the authentication process.

scollinson commented 10 years ago

Hi @bjubinville, As you can see in my accepted pull request (https://github.com/witoldsz/angular-http-auth/pull/36) you can now pass an updater function to the loginConfirmed function. This function returns a modified version of the request config to be retried. Here's an example:

authService.loginConfirmed(undefined, function(config) {
  config.headers = _.extend(config.headers, {'Authentication-Token': response.token});
  return config;
});

Hope that helps. Let me know if you have any more troubles.

utilityboy commented 10 years ago

Perfect, thanks much @scollinson (and again thanks @diego-vieira).