mongodb-js/connect-mongodb-session

Session saved in the DB but req.session result is {"cookie":{"originalMaxAge":172800000,"expires":172800000,"secure":false,"httpOnly":true,"path":"/"}}

reyontech opened this issue · 9 comments

var store = new MongoStore(
{
uri: process.env.ATLAS_URI,
collection: 'sessions'
},
function(error) {
// Should have gotten an error
});
store.on('error', function(error) {
assert.ifError(error);
assert.ok(false);
});

app.use(session({
secret: 'abcdefghijklm',
saveUninitialized: true,
resave: true,
cookie: {
secure: false,
expires: 1000 * 60 * 60 * 24 * 2
},
store: store
}));

app.post('/cart', function(req, res){
var cart = req.body;
req.session.cart = cart;
req.session.cart = cart;
req.session.save(function(err){
if(err){
throw err;
}
res.json(req.session.cart);
});
})

app.get('/', function(req, res) {
res.send(req.session);
});

image

I don't understand the issue, can you please clarify what the expected behavior is and what the behavior you're seeing is?

Session stored in the DB working fine.

This part not working. req.session
app.get('/', function(req, res) {
res.send(req.session);
});

Reult is
{"cookie":{"originalMaxAge":172800000,"expires":172800000,"secure":false,"httpOnly":true,"path":"/"}}

My session data not visible here.

Same issue here.

// api/auth:login
router.post(apiRoutesBack.AUTH.LOGIN, async (req, res) => {
        req.session.user = {
            id: 'someId'
            email: 'someEmail',
            username: 'someUsername',
        };

        res.json({ user: req.session.user });
});

And on app load, in my initmethod,

// api/auth:load
router.get(apiRoutesBack.AUTH.LOAD, async (req, res) => {
        console.log('req.session', req.session);
        res.json(req.session);
});

console.log('req.session', req.session); always prints:

req.session Session {
        cookie: {
            path: '/',
            _expires: 2020-09-18T07:13:43.733Z,
            originalMaxAge: 604800000,
            httpOnly: true,
            secure: null,
            domain: null,
            sameSite: null
      }
}

My setup in server.ts:

import express from 'express';
import next from 'next';
import { authRouter } from './routes';
import { connectDb } from './utils/connectDb';
import session from 'express-session';
import { v4 } from 'uuid';
import MongoDBStore from 'connect-mongodb-session';
import config from 'config';

const sessionStore = MongoDBStore(session);
const sessionInit = {
    secret: 'This is a secret',
    cookie: {
        maxAge: 1000 * 60 * 60 * 24 * 7,
    },
    resave: true,
    saveUninitialized: true,
    genid: () => v4(),
};

const PORT = process.env.PORT || 3000;

const dev = process.env.NODE_ENV !== 'production';
const nextApp = next({ dev });
const handle = nextApp.getRequestHandler();

const applyRoutes = (app: Application) => {
    app.use('/api/auth', authRouter);
};

const startServer = async () => {
    await nextApp.prepare();
    await connectDb();

    const app = express();

    if (!dev) {
        app.set('trust proxy', 1); // trust first proxy
        sessionInit.cookie.secure = true; // serve secure cookies
    }

    sessionInit.store = new sessionStore({
        uri: config.get('mongoURI'),
        collection: 'sessions',
    });

    app.use(session(sessionInit));

    app.get('*', (req, res) => handle(req, res));
    app.listen(PORT, () => console.log(`App listening on port ${PORT}`));
};

startServer();

The sessions collecting gets created on MongoDB Atlas, but the cookie seems to not persist through page reloads and page navigations.

Same for me. The session gets stored in the database, but not in req.session.

Is there a special way to update the session for req.session?

Ended up using pure cookies (storeless). next-iron-session. Just iron-session would work fine as well.