I’m developing a node.js app where I’m using passport to build OAuth authentication system. I can access the user through request object in each resource load after configurating it. But my question is: How can I do to check before every URL load – resources defined with app.get(‘/’, function(..) {…}) – if user is loged and redirect the client if it’s not loged. I could do it just adding a test in every method, but this is not what I want.
Thanks!
You want a middleware that checks whether the user is logged in.
And to use it on every route that needs to be logged in:
You can also use the middleware on every route, by doing
app.use(isAuthenticated), but you will need to have extra logic in the method to not create infinite redirect loops to/login, etc.