agentcooper / react-pdf-highlighter

Set of React components for PDF annotation
https://agentcooper.github.io/react-pdf-highlighter
MIT License
973 stars 388 forks source link

Adding JWT to request headers when PDF is retrieved #256

Closed ausaf-a closed 6 months ago

ausaf-a commented 7 months ago

I'm retrieving PDFs from my backend. Right now it's working since I have no authentication protecting the /getpdf/{filename} route, but I'm planning on adding it.

How do I get PDFLoader to add headers? I could also just sent the JWT as a query param, but I don't think this is recommended practice.

DanielArnould commented 7 months ago

The PDFLoader passes all props to PDF.js when loading the document. That means any of the properties in the DocumentInitParameters type can also be used as props.

For headers, that means you could do something like this:

const jwtToken = "PUT YOUR JWT TOKEN HERE"
const headers = {
  'Authorization': `Bearer ${jwtToken}`,
  // Add other headers as needed
};

<PdfLoader url={url} httpHeaders={headers}>
    // Your existing code like normal
</PdfLoader>

Hopefully that works 👍!

ausaf-a commented 6 months ago

Works!