I am trying to use authenticateUser () middleware before loading all of my pages. Instead of including it in every call (as in app.get ('/', authenticateUser, function () ...)), I tried to configure it with app.use (authenticateUser) right before calling app.use ( app.router).
However, this did not work. authenticateUser is basically:
if (req.session.loginFailed) { next() else { if (req.session.user_id) { ... if (userAuthenticated) { next(); } else { req.session.loginFailed = true; console.log('setting loginFailed to true'); res.redirect('/login'); } } }
And then in app.get ('/ login') I set req.session.loginFailed as false,
This should work, but I only want to call it app.get () or app.post (), etc. for one of my actual pages. I think its call is called many times for many different requests (because when loading one page, "setting loginFailed to true" is called many times)
Is there a better way to do this? Or should I just call it before every page on my site?
source share