'How to get the current HTTP basic auth headers?
I have a staging site that requires the user to enter HTTP basic auth credentials before accessing the site:
const basicAuth = require('express-basic-auth')
app.use(
basicAuth({
users: { fee: 'bar' },
challenge: true,
})
)
When I first visit the site I correctly get the prompt asking me to enter the basic auth credentials:
However, after I enter the credentials and try to make a fetch request to the server I need to re-send over the basic auth credentials but how do I get them? I don't want to hardcode them in. Is there a way to access the already used credentials from the browser somehow and send those over instead of replacing them? (Note this JS is inside a webworker):
const response = await fetch('someURL/', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
Authorization: 'Basic ' + btoa(username + ':' + password), <-------------------?
},
}).then((response) => response.json())
If I just remove the Authorization header altogether the site won't let me through without the credentials, so the browser is not appending them onto my request.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

