Search code examples
node.jsexpressnode.js-connect

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


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...


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.