NodeJS + Express + Mongo Session storage
Asked Answered
A

2

6

I am currently having a hell of time trying to store sessions in MongoDb.

I've tried express-session-mongo and connect-mongodb and both give me the same "500 internal server error" when I try to load the login page. Which leads me to think maybe there is a conflict with mongoose-auth somewhere.

Anyway here is my setup:

app.js:

var MongoStore = require('connect-mongodb');
var MongoDb = require('mongodb').Db;
var Server = require('mongodb').Server;

var db = new MongoDb('myDb', new Server('localhost', 27017, {auto_reconnect: true, native_parser: false}));

app.configure(function() {
  app.use(express.logger({format: 'dev'}));
  app.set('views', __dirname + '/../views');
  app.set('view engine', 'jade');
  app.set('view options', { layout: false });
  app.use(express.bodyParser());
  app.use(express.cookieParser());
  app.use(mongooseAuth.middleware());
  app.use(require('./mysite').middleware());
  app.use(express.methodOverride());
});


app.configure('production', function(){
  var oneWeek = 657450000;
  app.use(express.static(__dirname + '/../public', { maxAge: oneWeek }));
  app.use(express.session({ store: new MongoStore({db: db}), secret: 'super secret' }));
  app.use(express.errorHandler());
});

// Routes
require('./routing.js');

mongooseAuth.helpExpress(app);

app.listen(3000);
console.log('Express server listening on port %d in %s mode', app.address().port, app.settings.env);

userModel.js

var Schema = mongoose.Schema;
var mongooseTypes = require("mongoose-types");
var mongooseAuth = require('mongoose-auth');
mongooseTypes.loadTypes(mongoose);

var everyauth = require('everyauth')
everyauth.debug = true;

var UserSchema = new Schema({any: {}});

UserSchema.plugin(mongooseAuth, {
    everymodule: {
      everyauth: {
          User: function () {
            return User;
          }
      }
    }
    , password: {
      loginWith: 'email'
      , extraParams: {
            phone: String
          , username: String
        }
      , everyauth: {
            getLoginPath: '/login'
          , postLoginPath: '/login'
          , loginView: 'account/login.jade'
          , getRegisterPath: '/register'
          , postRegisterPath: '/register'
          , registerView: 'account/register.jade'
          , loginSuccessRedirect: '/login/success'
          , registerSuccessRedirect: '/register/success'
        }
    }
});

mongoose.model('User', UserSchema);
User = mongoose.model('User');

At this moment in time I'm really just trying to use MongoDb as the session store, but again I get a 500 error w/ no information whatsoever in the node.js console when I try to load the login page.

Any help would be greatly appreciated.

Thanks

Anklebone answered 1/3, 2012 at 22:12 Comment(3)
Actually I just retried with express-session-mongo and although I'm still getting the same behavior (trying to load the login page fails w/ a 500 error), I just checked in MongoDb and the session is actually there as expected.Anklebone
FYI, also did try connect-mongo, same result... mongoose-auth not playing nice?Anklebone
After some digging into the everyauth module's code, all I was able to figure out was that the 500 error happened in everyauth/lib/modules/password.js >> line 47: res.render(view, locals); Unfortunately that doesn't help too much from where I stand... :(Anklebone
A
10

It ended being a problem of the various modules: connect-session-mongo / express-session-mongo / connect-mongo, using connect 2.0.1 and Express using connect 1.8.5.

Apparently the dependency clash here prevented the session store modules to access the 'req.secret' property.

To make it work I ended using the module connect-mongodb that is still using connect 1.8.5, just like Express.

The reason I couldn't make connect-mongodb work before though was user error, I tried too hard to use copy/paste from online examples instead of my head.

Here is the configuration code that ended up working for me with connect-mongodb:

var Session = require('connect-mongodb');

app.configure('production', function(){
  var oneWeek = 657450000;
  app.use(express.static(__dirname + '/../public', { maxAge: oneWeek }));

  var session = express.session({
        store: new Session({
              url: 'mongodb://localhost:27017/test', 
              maxAge: 300000
        }),
        secret: 'superTopSecret' 
  });
  app.use(session);

  app.use(mongooseAuth.middleware());
  app.use(require('./mySite').middleware());
  app.use(express.methodOverride());
  app.use(express.errorHandler());  
});

Hope this helps anyone else who runs into this issue. If you have any suggestion/improvement on this solution, I'd be glad to hear it. :)

Anklebone answered 2/3, 2012 at 20:38 Comment(2)
Thanks for this, helped me out. I was also able to use Mongoose's existing connection instead of specifying the URL. Which is especially nice when you have multiple environments with separate DB instances. Just replace the url param with: db: mongoose.connection.dbWhom
I just want to say, you're great.Mixup
M
1
const express = require('express')
const app = express()
const cookieParser = require('cookie-parser');
const session = require('express-session')
const mongoose = require('mongoose');
const MongoStore = require('connect-mongo')(session);

mongoose.connect('mongodb://localhost/my-database', {
    useMongoClient: true
});
mongoose.Promise = global.Promise;
const db = mongoose.connection

app.use(cookieParser());
app.use(session({
    secret: 'my-secret',
    resave: false,
    saveUninitialized: true,
    store: new MongoStore({ mongooseConnection: db })
}));
Musa answered 22/3, 2021 at 18:14 Comment(1)
While this might answer the question, if possible you should edit your answer to include a short explanation of how this code block answers the question. This helps to provide context and makes your answer much more useful to future readers.Vassell

© 2022 - 2024 — McMap. All rights reserved.