How to load a session by userid using node.js/express/passport/redis?
-
21-12-2019 - |
Question
I have an application which uses redis as session storage, passport for authentication, everything on top of express.
When a user logs in, the object stored in cache has a structure similar to this one:
{
cookie : {cookie info, expiration, etc},
passport : { user : 'userid' }
}
My question is: is there a simple way to load that session (or session id) inside the node.js app, by providing only the 'userid' field? (e.g. loadSession(userid)
, where loadSession
returns the session id or the session itself).
A native Passport/Express/Connect-Redis alternative is preferred.
My intention is to invalidate a certain session in a given moment, without requiring the user to make an HTTP request to my app, i.e. without req.session
.
Thanks in advance.
Solution
As mentioned in the comments, if the userid
is all you have, you'll need to add node-redis
module and search for the record with that userid
— it can't be done with connect-redis
alone as that module is ONLY designed for session handling.