Currently, I am developing a web application using Angular that utilizes a JWT token for authentication. This means that every request must contain an "Authentication" header with all the required information.
While this method works well for REST calls, I am facing a challenge when it comes to handling download links for files stored on the backend server (where the web services are also located).
Traditional <a href='...'/>
links cannot be used as they do not carry any header information, causing authentication to fail. The same applies to methods like window.open(...)
.
I have considered a few possible solutions:
- Generate a temporary unsecured download link on the server
- Include the authentication details as a URL parameter and handle the situation manually
- Retrieve the file data via XHR and save it on the client side.
However, none of the above options seem entirely satisfactory.
As of now, I am using option 1, but I am not fully content with it due to security concerns and the substantial amount of work involved on the server side. Generating a new "random" URL, storing it in a database, and maintaining its validity require significant effort.
Option 2 appears to be equally labor-intensive.
Option 3 involves considerable work, even with available libraries, and presents potential complications such as managing a download progress bar, loading the entire file into memory, and prompting the user to save the file locally.
This task seems relatively straightforward, so I am exploring simpler alternatives that could offer a more efficient solution.
Any suggestions or recommendations would be greatly appreciated, regardless of whether they align with "the Angular way" or involve regular JavaScript techniques.