My objective is to include a header with each HTTP request for an ngResource (specifically, an auth token).
This solution somewhat accomplishes that:
app.factory('User', ['$resource','$window',
function($resource,$window,liftHost, liftBasePath) {
return $resource('/api/users/:id',{},{
get: {
method: 'GET',
headers: {'token': $window.sessionStorage.token}
}
});
}]);
The issue with this code is that after the initial call, subsequent GET requests will have the same header value. The header does not get updated dynamically. So, if a user logs out and then logs back in, changing the value of $window.sessionStorage.token, the request will still use the old token.
I have put together a small plunker using $httpBackend mocks to demonstrate this problem.
Check it out here: http://plnkr.co/edit/xYZM6wlDJ6CH2BPHLZAC?p=preview