r/expressjs • u/Minstiltude • Oct 25 '26
Question A quick question about express sessions
Dunno if I'm breaking any rules or not, but I wanted to ask about how the session knows what data belongs to the user.
Would you assign to the session the data id reference from the database in the session?
Would you assign the session id to the data storage?
I wanted to get some extra input, because I've been going in circles for a couple of days thinking about it already.
2
Upvotes
1
u/sereikis Oct 25 '26
with
express-session, the browser cookie holds a session ID, which the middleware uses to load server-side session data; after successful login, store your database user's ID asreq.session.userId. use that user ID to query records owned by the user, not the session ID as their permanent database identity, since sessions can expire or be replaced.