mrSidX
mrSidX

Reputation: 357

Trying to view all logged in Users in Express.js Node.js Passport.js

Trying to see all sessions of all logged in users in Express Passport, and want be able to see who is currently logged in. Whats the best and quickest means?

I was thinking maybe I could do this at login and update the user model db 'online' boolean variable to true. Then I can run a check on the Users.find() for online=true...

But how will I check this during the event of user logging off or their session ending?

Unless there is an easier way to do this without writing to the db?

I'd eventually like the user to maintain the option to opt out of being viewable online.

Upvotes: 4

Views: 3811

Answers (1)

Captain Crocus
Captain Crocus

Reputation: 76

In the case of using express-session (if store is default value: MemoryStore) you can get store object and sessions array from req object:

var sessions = req.sessionStore.sessions

For example (for me) it looks like that:

{
    KT29eOp9XkzFfXWdz_UVUQMI4Qf3VTxE: 
        '{"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"},
        "passport":{"user":"5a95b34b52670e19200ec48e"}}',
    WW7BAhNNBbuFUsKQ0dbalgToEEfGES8p: 
        '{"cookie":{"originalMaxAge":null,"expires":null,"httpOnly":true,"path":"/"},
        "passport":{"user":"5a9d56a18149111cf0556991"}}' 
}             

user values in passport props are _id of your users in MongoDB User collection.

Upvotes: 4

Related Questions