1

I have problem with creating node.js application based on express with http-auth module.

It's a possible create Middleware for this http-auth library I have this code:

    //Configure Middlewares
logger.configure(function() {

  //All output has content type json
  logger.use(function(req, res, next) {
    res.contentType('application/json');
    next();
  });

  //Create digest auth middleware
  logger.use(function(req, res, next){
    digest.apply();
    next();
  });
});

After applying this, when I connect to site I get this error:

TypeError: Cannot read property 'headers' of undefined

Is there any solution for this problem or use another method?

I need digest auth for the whole application.

Håken Lid
  • 22,318
  • 9
  • 52
  • 67

2 Answers2

1

I think apply() is expecting the request object (hence it can't read the headers);

try this syntax:

digest.apply(req, res, function(username) {

});   
UpTheCreek
  • 31,444
  • 34
  • 152
  • 221
0

You should just use it with correct pattern:

// Authentication module.
var auth = require('http-auth');
var digest = auth.digest({
  realm: "Simon Area.",
  file: __dirname + "/../data/users.htdigest"
});

// Configure Middlewares
logger.configure(function() {
  // Create digest auth middleware
  logger.use(auth.connect(digest));

  // All output has content type json.
  logger.use(function(req, res, next) {
    res.contentType('application/json');
    next();
  });    
});
gevorg
  • 4,835
  • 4
  • 35
  • 52