joseluisq / printd

Print HTML elements or pages in modern browsers.
MIT License
80 stars 5 forks source link

Pass Authentication Headers via printURL. #9

Closed kieranbarlow closed 5 years ago

kieranbarlow commented 5 years ago

Is it possible to pass headers to the printURL() method? I'm testing printd in a single page application and the endpoint is locked behind JWT authentication.

I could attach the token to the url.

https://endpoint/api/testing?token=JWT TOKEN

I feel a more elegant approach would be better.

joseluisq commented 5 years ago

Is it possible to pass headers to the printURL() method?

No, it's no possible. Because if you see we are setting the URL directly to the iframe. So I think your query token solution is suitable for.

But afterwards (after page load) you can do it for future requests if you want via the callback.

https://github.com/joseluisq/printd/blob/ea151cfa8b587cf2e840bcac9f0af83208d2ebc4/src/index.ts#L48-L55

Remember that the callback receives those params above. So , for example you can leverage the iframe param and append all headers you want.

Solution from your side

Use a custom XHR request for fetch the page using your headers and then pass just the content to the iframe via print() method.