Search code examples
javascripthtmlajaxbasic-authentication

Javascript - Using window.open() with Basic Authentication


When the user logs into my app, he can click on a button to download a CSV file from the server. This URL for the download is protected using basic authentication. When I do this :

window.open('http://url-of-the-csv-file');

The browser shows a username/password popup (Basic Authentication)

I want to be able to open the URL without asking the user to enter his password a second time. How do I do that ?


Solution

  • If you know basicAuth credentials of your looged-in user, you could fill in them in the URL:

    https://user:password@url-of-the-csv-file
    

    If your app is in open web, do it ONLY if the target is on HTTPS, because such request including credentials will be readable for anyone eavesdropping your wires. This also applies to the other (javascript) answer.