#StackBounty: #javascript #node.js #express #express-session #connect-mongo express session not saving for Ipad

Bounty: 50

I am trying to save a session variable for a user when they login. This works on the computer, but when I try it on an iPad using Safari or Chrome it doesn’t save.

Here is where I set up my session:

app.use(session({
  secret: crypto.randomBytes(20).toString('hex'),
  resave: false,
  duration: 60 * 60 * 1000,
  activeDuration: 10 * 60 * 1000,
  saveUninitialized: false,
  cookieName: 'session',
  cookie: { secure: true }
}))

I use this route to set up the user:

.get('/checkLogin', (req,res) => {
  const loginCred = req.query;
  db.any('SELECT * FROM users WHERE user_name = $1 AND password = $2 LIMIT 1', [loginCred[0], loginCred[1]])
  .then(function (user) {
    req.session.user = user;
    req.session.save();
    res.end(JSON.stringify(user));
  })
  .catch(function (err) {
      throw err;
  })
})

When I console log this, it is getting set properly.
Then when I call the session on the return it is not there. I’ve tried to add the save and that still didn’t work. I also added maxage to the session variable to keep it alive for 3 days and it still didn’t work.

UPDATE

Changing the options has not solved this issue. I was thinking I would try to switch to connect-mongo to try and store my sessions but I cannot seem to configure it right. And I cannot seem to find any tutorials that work.

I have added this:

var MongoStore = require('connect-mongo')(session);

app.use(session({
  secret: crypto.randomBytes(20).toString('hex'),
  store: new MongoStore({
    url: 'mongodb://localhost/test-app'
  })
}))

I do not understand how to set up the mongo DB. The tutorials make it seem like it will set itself up but it doesn’t for me. When I run it, I get this:
Error: Connection strategy not found


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.