20
I'm using passport to handle authentication and sessions in my application. I'm persisting sessions to mongodb using mongostore.
The setup works fine in general. However, when I restart the server all users are logged out, so apparently sessions are hold in memory instead of being only persisted to mongodb. I'm trying to achieve a setup where users are still logged in when restarting the server.
Basic configuration is as follows
middleware
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(passport.initialize());
app.use(passport.session({
maxAge: new Date(Date.now() + 3600000),
store: new MongoStore(
{
db: mongodb.Db(
conf.mongodbName,
new mongodb.Server(
'localhost',
27017,
{
auto_reconnect: true,
native_parser: true
}
),
{
journal: true
}
)
},
function(error) {
if(error) {
return console.error('Failed connecting mongostore for storing session data. %s', error.stack);
}
return console.log('Connected mongostore for storing session data');
}
)
}));
passport
passport.use(new LocalStrategy(
{
usernameField: 'email',
passwordField: 'password'
},
function(email, password, done) {
console.log('user %s attempting to authenticated', email);
return User.findOne({email:email}, function(error, user) {
if(error) {
console.error('Failed saving user %s. %s', user.id, error.stack);
return done(error);
}