Node.js – How to find the session Id when using express / connect and a session store

expressnode.jsredis

If a user is already logged in and tries to login again in a new instance I'd like it to log out the other user instance. I don't want the same user to be logged in twice on my application.

Currently the session is stored in a Redis store, i'm using express / connect to handle the session storage. One of the functions available which could be used to destroy the session is as follows:

.destroy(sid, callback)

However I need to find that session id before I call .destroy(). In Redis the username is stored as a part of the session.

Question: Is it possible to query Redis to obtain the session id based on the username?

Best Answer

req.sessionID will provide you the session's ID, where req is a request object.