node.jsexpressbasic-authenticationnode.js-connect

Expressjs with Connect basic auth won't set request user


I'm using express and adding in basic authentication via the connect middleware. I'm trying to use the async version and it claims the user property will be set on the request object when calling using the async version.

If I call the proper fn(err, obj); with an object then the basic authentication passes and moves onto my routes, but I want to have the req.user set when it gets to my route.

Here is the connect doc on basic auth.

Am I not calling the callback properly?

app.use(express.basicAuth(function(user, pass, fn){

    db.getUserByEmail(user, function(err, obj){

      if (err) sendError(500, req, 'error', err);
      else if (obj == null) fn(err, obj); 
      else if (obj.password == pass) fn(null, obj); 
      else fn(null, null); 

    });
  }));

Solution

  • I've checked code in docs, you've attached, it already set up req.user, so, everything should work fine if this is exactly the same code executed as in docs :)

    BTW, why not use something more friendly, and well-documented like passportjs?