2

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

    });
  }));
yatskevich
  • 2,085
  • 16
  • 25
Patrick Kafka
  • 9,795
  • 3
  • 29
  • 44
  • 2
    Not a solution, but have you seen [passportjs](http://passportjs.com)? A very well documented authentication framework with all necessary strategies. – Patrick Mar 20 '12 at 08:02
  • Thanks, I like the look of passport. Funny though, passport is just a skin on Connect, and [here](http://passportjs.org/guide/authenticate.html) is my issue where it is setting req.user. Maybe it will work having it go through passport? I'll try. – Patrick Kafka Mar 20 '12 at 16:18
  • it works, and having documentation was nice. thanks. – Patrick Kafka Mar 20 '12 at 16:51

2 Answers2

1

Unlike mentioned in the documentation for connect, in express the authenticated user will be assigned to req.remoteUser

Dave
  • 11
  • 1
0

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?

Anatoliy
  • 29,485
  • 5
  • 46
  • 45
  • 1
    Thanks, things seem to work when I use passport, even though it's basically connect with a skin on it. The documentation makes all the difference. – Patrick Kafka Mar 20 '12 at 17:09