Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 806
mrSidX Avatar asked Oct 17 '25 20:10

mrSidX


1 Answers

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.

like image 78
Captain Crocus Avatar answered Oct 19 '25 13:10

Captain Crocus



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!