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.

2
  • logger is instance of express application. Commented Mar 13, 2013 at 12:53
  • Just use proper pattern for http-auth integration with connect.
    – gevorg
    Commented Jun 5, 2016 at 11:06

2 Answers 2

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) {

});   
3
  • This might work but when i want go to any route like, i have empty response. Commented Mar 13, 2013 at 13:16
  • The apply function is asynchronous, so you'll probably have to call next() in the apply callback. Of course what you describe could be something unrelated.
    – UpTheCreek
    Commented Mar 13, 2013 at 13:26
  • This answer is not valid anymore for http-auth module.
    – gevorg
    Commented Jun 5, 2016 at 11:05
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();
  });    
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.