I’m using express-session on the backend using MongoDB store. I can see the session is being stored in the db. Session variable holds the value until I redirect. After the redirect even tho the value is still there in db, each new request has session variable as null.
The setup I’ve works perfectly fine on my local. The same config works on my local but not in Production on Vercel.
Solutions I tried:
- resave – tried setting this to true but MongoDb store uses touch command
- saveUninitialized – tried both true and false.
.save()
before the redirect.
This is session setup in express:
const store = new MongoDBSession({
uri: process.env.DATABASE_URL,
collection: 'users' })
app.use(session({
secret: process.env.SESSION_SECRET,
resave: false,
saveUninitialized: true,
store: store,
cookie: {
originalMaxAge: parseInt(process.env.SESSION_MAX_AGE),
httpOnly: false, // on my local I set this to true
},
})
);
Router:
router.route('/callback').get(async (req, res) => {
await auth.initUserConnection(req);
req.session.isAuth = true;
req.session.save(() => {
res.status(200).redirect(`${process.env.VERCEL_FRONTEND}/problems`); //redirecting to frontend.
});
});
this route session variable doesn’t exist anymore:
router.route('/execute').get((req, res) => {
res.status(200).json({isSignedIn : req.session.isAuth}); //isAuth is null here
});
I’m not sure if my config is wrong or why the session doesn’t persist when I’ve the exact same setup working fine on local.