I need help understanding the concept of sessions for a web application. I am running a Node.js server with Express 3.0.
My goals are to:
-
Create a session for each user that logs in
-
Store this session and use it for validating if the user is already logged in (prevent two devices using the same user at the same time) and to limit access to certain pages (by matching session ID to some other data)
I will be using MemoryStore to save the sessions (seems easiest). If the above goals make sense can you provide a thorough explanation of how to achieve them?
Express has nice examples in the github repo. One of them deals with authentication and shows how to attach the user to the
req.sessionobject. This is done inside theapp.post('/login')route.To limit access to certain pages add a simple middleware to those routes
As Brandon already mentioned you shouldn’t use the MemoryStore in production. Redis is a good alternative. Use connect-redis to access the db. An example config looks like this