I have a node.js app that used to work when basicAuth was a module of node. Below code would popup a password and username prompt when visiting app.
// start the UI
var app = express();
app.use(express.basicAuth('me', 'openforme'));
app.use(kue.app);
app.listen(3001);
Now that basicAuth was removed as a module, from node, I am using express-basic-auth. When using the below code I get a 401 unauthorized error because it is not giving me a username and password popup prompt like basicAuth did?
// start the UI
var app = express();
var basicAuth = require('express-basic-auth');
app.use(basicAuth({
users: { 'me': 'openforme' }
}));
app.use(kue.app);
app.listen(3001);
1 Answer. It is indeed not possible to pass the username and password via query parameters in standard HTTP auth. Instead, you use a special URL format, like this: http://username:[email protected]/ -- this sends the credentials in the standard HTTP "Authorization" header.
We can do HTTP basic authentication URL with @ in password. We have to pass the credentials appended with the URL. The username and password must be added with the format − https://username:password@URL.
Explanation: The first middleware is used for checking the authentication of the client when the server start and the client enter the localhost address. Initially req. headers. authorization is undefined and next() callback function return 401 status code unauthorized access to the browser.
Now there is an out-of-the-box option from express-basic-auth
. The Challenge property.
app.use(basicAuth({
challenge: true,
users: { 'me': 'openforme' }
}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With