简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM