Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get express-basic-auth to prompt a popup for user and password?

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);
like image 679
jdog Avatar asked Feb 13 '18 15:02

jdog


People also ask

How do I pass username and password in basic authentication?

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.

How can I pass the basic HTTP authentication?

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.

How do I give a basic auth in node JS?

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.


1 Answers

Now there is an out-of-the-box option from express-basic-auth. The Challenge property.

app.use(basicAuth({
    challenge: true,
    users: { 'me': 'openforme' }
}));
like image 144
Boyko Karadzhov Avatar answered Nov 15 '22 22:11

Boyko Karadzhov