Search code examples
expressstormpathexpress-stormpath

Any issues with adding a match all route that grabs the user on every request?


Interested in grabbing the user on every request so that I can have funtionality even on pages that don't have any stormpath functionality middleware.

Any issues with this? If user is logged in it returns the user in the request object, if the user is not logged in, it returns undefined. which is exactly what I want. Any 'gotchas' I'm missing? It 'seems' to work great.

 app.get('*', stormpath.getUser, function(req, res, next) {
  next()
});

Solution

  • That's fine, although your code won't cover all routes & http methods. It's probably easier to do this:

    app.use(stormpath.getUser)

    Since in express, all route handlers are "middleware", you can pass stormpath.getUser directly into the handler without the function calling next().

    Also, matching all GET requests by using * will miss any POST, DELETE, PUT, etc requests. app.all will match all routes and all HTTP methods.