I have some API endpoints which provide images, but said images require authentication. I have a service (authenticatedHttp) which is an abstraction over $http and handles the authentication token management. (This is confirmed to be working, and for all intents and purposes might as well be $http)
I am receiving the response, getting a blob and creating an object URL. All of this works. I have checked the blob and can confirm it is correct.
The problem comes in when I try to apply objectUrl to src. I have tried a simplified example using a snippet I know works and no authentication. This does not work in my Angular app, but does work in vanilla JS.
I don't know much about ObjectUrl but if it makes a difference, when I inspect the empty image I see that it's src is correct, and that if I click the link created (ex. blob:https://sub.domain.dev/de4db0e0-77c8-44bc-a934-0d270ab81687) I get redirected by ui-router.
Why is this happening and how can I correct it?
app.directive('authenticatedSrc', ['authenticatedHttp', function (authenticatedHttp) {
var directive = {
link: link,
restrict: 'A'
};
return directive;
function link(scope, element, attrs) {
var requestConfig = {
cache: 'false'
};
authenticatedHttp.get(attrs.authenticatedSrc, requestConfig).then(function(response) {
var objectUrl = window.URL.createObjectURL(new Blob([response.data], { type: 'image/png' }));
attrs.$set('src', objectUrl);
});
}
}]);