I'm making a SPA with login functionality. I enacted session handling with 'express-session' originally so that the user's mongoDB _id would be stored, and they could hit 'refresh' without being logged out, which worked perfectly well.
From here, I want to allow the use to check a "remember me" box that will control long-term site (re)visits. So, if 'remember me' is not checked, during a given visit, the session handling works the same ('refresh button' maintains login), but session data is lost once they close the browser. Conversely, checking 'remember me' stores their login data in my database (using 'connect-mongo' for this), maintaining login between full server visits (days, weeks, etc.).
The problem seems to be that the browser's session cookie and mongodb session entry are intrinsically tied together, so I can't alter/eliminate one without affecting the other. If that's even the right approach?
So here I get the everything I need:
var express = require('express');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var fs = require('fs');
var bodyParser = require('body-parser');
var dbHdlr = require('./Server/dbHdlr.js')();
... set up session handling with storage...
var mongoStore = new MongoStore({
url: 'mongodb://' + dbHdlr.GetConnectionURL(),
ttl: 7 * 24 * 60 * 60, // Session saves for 7 days
});
var SessionFunc = session({
secret: 'SomeKindOfSessionSecretIReallyDontKnowMuchAbout',
cookie: {
maxAge: 7 * 24 * 60 * 60 * 1000 // Cookie expires in 7 days
},
saveUninitialized: false,
resave: false,
store: mongoStore
});
app.use('/' , SessionFunc);
... Apply login functionality with 'remember me' checkbox (using "persist" property in this context to mean long-term persistence) ...
app.post('/LoginData', function(req, res){
dbHdlr.GetAccountID({ email: req.body.email, password: req.body.password }, function(resObj) {
if(resObj.success) {
req.session.accountID = resObj.accountID;
if(req.body.remember)
req.session.persist = true;
else
req.session.persist = false;
}
res.json(resObj);
});
});
... a single point of entry where i create a page initially based solely on login status. And this is where I figured that if they choose not to have long-term persistence, I would simply remove the entry from the store? Or reset the ttl to one second maybe? I don't know how to make this work. Removing the store entry kills the whole thing so that I can't even hit 'refresh' without being logged out, even though that wasn't a problem before the store was ever even implemented ...
app.get('/', function (req, res) {
if(req.session.accountID) {
res.render('index.ejs', { loggedIn: true });
if(!req.session.persist)
mongoStore.destroy(req.sessionID, function(error) {});
}
else
res.render('index.ejs', { loggedIn: false });
});
Thoughts? Is this possible to achieve without explicitly programatically knowing when the user hits 'refresh' versus changing sites/closing the browser?