I am allowing users to download files from my application. The files are private and require auth to access them.
Currently, The frontend requests a short lived token from the server, and appends it to the URL, so that the server can verify that authorized user is trying to downlod it. This token is generated on the backend, and a new token needs to be fetched every time the user wants to download a file. Based on this new token, the download url is decided.
This leads to some problems:
- Since I don't have the url available unless the user clicks on Download, I can't have the link href pointing to the correct file path by default.
- I would like the user to just click on download link and the download should start. This is why I am trying to look for a way to set link href after click. I would start with an empty href and onclick it would set the correct href and navigate, which would start the download. This would be fine if there was no asynchronous call involved, but here, I have to wait for the token, then change the href before going ahead with the navigation.
In all, I am trying to understand if there is a way to pause the event and resume it after the token is available. If I preventDefault, is there some way to resumeDefault. Or may be, the functionality I want is to wait for a promise to resolve before navigating using the link.
Some alternative approaches:
- Provide a button for "generating download link" , on click of this button, get the download token and create a new link with appropriate href and display on the UI. The user can now click this link to download the file.
- When the original link is clicked, preventDefault, get token, create temporary link, and click this temporary link programatically.
- use window.open when the promise returns.