How do you maintain session between route.
I have 3 routes
var routes = require('./routes/index');
var users = require('./routes/users');
var question = require('./routes/question');
app.use('/', routes);
app.use('/users', users);
app.use('/question',question);
on top of the 3 js file is this:
var express = require('express');
var router = express.Router();
this created a new router thus losing the session i guess?
within users, i use passport to create a login system, by its default serializer the user information is saved under req.user but only accessible within users route.
I would like to use the session within routes(index) and question route. How do i solve this?
thanks,
As you mentioned that you have defined each request specific route in its separate file. Then you donot need to define these line again in main file.
var express = require('express'); var router = express.Router();
app.use(passport.session());
function ensureAuthenticated(req, res, next) {
// passport.js provides this method req.isAuthenticated())
if (req.isAuthenticated())
return next();
else
// Return error content: res.jsonp(...) or redirect: res.redirect('/login')
}
Here, you can define your strategy to check routes. If it is authenticate then disclose user related information.
app.use('/', ensureAuthenticated, routes);