Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

How to monkey patch request obj with data in express/connect

How to monkey patch request obj with data in express/connect

Problem

Helllo Im programming a middleware library to abstract the process of oauth authorize/authenticate and supply own authentication strategies.

I'm stuck because when I Monkey Patch the request with data it won't be available on any routed request but the other middlewares (connect/express documentation is very poor on this subject).

How can I make the data stick so it would be available to every request (except for static files)?

Thanks.

P.s I don't use other available modules because it was just frustrating to make them work...

Problem courtesy of: Cu7l4ss

Solution

Try this:

function authOrDie() {
  return function(req,res,next) {
    console.log(req.isAuthenticated());
    next(); // Have to call next to continue to the next middleware
  };
}

I assume console.log(req.isAuthenticated()); is being run and it's working, I also assume it's printing false.

Also, if requests to your static files doesn't need to be authenticated you should move it much higher in the stack.

Solution courtesy of: Ryan Olds

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

How to monkey patch request obj with data in express/connect

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×