How to directly call Connect middleware?

I have an express route:

app.get('/', auth.authOrDie, function(req, res) { res.send(); }); 

where the authOrDie function authOrDie defined like this (in my auth.js module):

 exports.authOrDie = function(req, res, next) { if (req.isAuthenticated()) { return next(); } else { res.send(403); } }); 

Now that the user is not authenticated, I would like to check if the HTTP request has an authorization header (Basic). For this, I would like to use the excellent basicAuth () middleware.

As you know, Express is built on top of Connect, so I can use express.basicAuth .

Commonly used by basicAuth :

 app.get('/', express.basicAuth(function(username, password) { // username && password verification... }), function(req, res) { res.send(); }); 

But I would like to use it in my authOrDie function as follows:

 exports.authOrDie = function(req, res, next) { if (req.isAuthenticated()) { return next(); } else if { // express.basicAuth ??? ****** } else { res.send(403); } }); 

****** How can I call the basicAuth function with good parameters (req? Res? Next? ...).

Thanks.

+4
source share
1 answer

Calling the express.basicAuth function returns a middleware function to call, so you call it directly as follows:

 exports.authOrDie = function(req, res, next) { if (req.isAuthenticated()) { return next(); } else { return express.basicAuth(function(username, password) { // username && password verification... })(req, res, next); } }); 
+6
source

All Articles