Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are connect-mongo MongoStore sessions actually saved?

I implemented sessions using Passport, but for storing sessions I tried connect-mongo using a mongoose connection.

This is my code (sessions part):

var express     =       require('express')
var mongodb     =       require('mongodb')
var mongoose    =       require('mongoose')
var bodyParser  =       require('body-parser')
var cookie      =       require('cookie-parser')
var connect     =       require('connect')
var passport    =       require('passport')
//var flash         =       require('connect-flash')
var session     =       require('express-session');
var MongoStore  =       require('connect-mongo')(session);
var LocalStrategy =     require('passport-local').Strategy;

var app = express()

var BSON = mongodb.BSONPure

app.use(express.static(__dirname+"/public"))
app.use(bodyParser())
app.use(cookie())
app.use(connect.session({ secret: 'ilovescotchscotchyscotchscotch' })); 
app.use(passport.initialize());
app.use(passport.session());

mongoose.connect('mongodb://localhost/psicologosTuxtepecDB')



var Schema = mongoose.Schema
var userCredential = new Schema({

    username:   String,
    password:   String

},  {
    collection:     'members'
})

var userCredentials = mongoose.model('members', userCredential)



app.use(session({
    secret: 'ziKologiia',
    clear_interval: 900,
    cookie: { maxAge: 2 * 60 * 60 * 1000 },
    store: new MongoStore({
      db : mongoose.connection.db
    })
  }));

Something I doubt if it would be counterproductive is that app.use(connect.session({ secret: 'ilovescotchscotchyscotchscotch' })) is usingconnect module but the MongoStore configuration is set upon a express-session variable. However, deleting first causes app to not work good (won't authenticate/redirect).

So, about my question title. Where is that session stored? I really thought I could go to my Mongo database and find any collection storing it.

How can I find such sessions at backend (Mongo) and even at the applicacion as Java Script objects?

like image 407
diegoaguilar Avatar asked May 21 '14 03:05

diegoaguilar


2 Answers

connect-mongo stores sessions in the "sessions" collection by default. They should be there and visible in the mongo shell or any GUI tool like robomongo. Yes, it is created by default. I would pass in the mongooose_connection option instead of db.

From the docs:

mongoose_connection in the form: someMongooseDb.connections[0] to use an existing mongoose connection. (optional)

like image 135
Peter Lyons Avatar answered Nov 04 '22 04:11

Peter Lyons


One thing you should do is replace

app.use(connect.session({ secret: 'ilovescotchscotchyscotchscotch' }));

with your

app.use(session({
  secret: 'ziKologiia',
  clear_interval: 900,
  cookie: { maxAge: 2 * 60 * 60 * 1000 },
  store: new MongoStore({
    db: mongoose.connection.db
  });
}));
like image 24
mscdex Avatar answered Nov 04 '22 04:11

mscdex