1

My API returns a PDF document for which I need my user to be authenticated. On my AngularJS application the user clicks on a link that calls a function:

 <a ng-click="getBankDetails()">Print bank details</a>

Controller:

$scope.getBankDetails = ->
$http.get(ENV.apiEndpoint + "/api/v1/users/get_bank_details", { headers: { 'Accept': 'application/pdf' }, responseType: 'arraybuffer'  })
   .success((resp) ->
     file = new Blob([resp], {type: 'application/pdf'})
     fileURL = URL.createObjectURL(file)
     window.open(file);
   )

but this doesn't work.

I have tried removing the authentication need on the API. The method above still doesn't work, but if I do

window.open(ENV.apiEndpoint + "/api/v1/users/get_bank_details")

that works. Not really helpful except to confirm that my API works fine.

I'm using ng-token-auth for authentification

it's stored in a cookie as I'm not using cordova here

$authProvider.configure
  apiUrl: ENV.apiEndpoint
  storage: if _.isUndefined(window.cordova) then "cookies" else "localStorage"

Any help would be greatly appreciated

Mike W
  • 391
  • 4
  • 14
  • I think you should be doing `window.open(fileURL);` instead of `window.open(file);`. – NiK648 Feb 16 '18 at 15:56
  • What kind of auth are you using? Token-Based oAuth? – lin Feb 16 '18 at 16:00
  • @NiK648 yeah it was a typo you're right, but still doesn't work – Mike W Feb 16 '18 at 16:43
  • I have done the exact same thing to display pdf but in my case the request method was `post`. Perhaps this article can help you: https://stackoverflow.com/questions/12876000/how-to-build-pdf-file-from-binary-string-returned-from-a-web-service-using-javas. – NiK648 Feb 16 '18 at 17:04

1 Answers1

0

If you are using a token based auth I would do it as simple as I can by adding the authToken into the URL to receive the file. Handle the authToken via Backend as GET-Param on that request:

$scope.getBankDetails = function () {
 window.open(ENV.apiEndpoint + "/api/v1/users/get_bank_details?authToken=XYZ");
}
lin
  • 17,956
  • 4
  • 59
  • 83