尝试查看Express.js Node.js Passport.js中所有已登录的用户 [英] Trying to view all logged in Users in Express.js Node.js Passport.js

查看:69
本文介绍了尝试查看Express.js Node.js Passport.js中所有已登录的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图查看Express Passport中所有已登录用户的所有会话,并希望能够查看当前正在登录的用户.最好和最快的方法是什么?

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?

我当时想也许可以在登录时执行此操作,并将用户模型db'online'布尔变量更新为true.然后,我可以在Users.find()上检查online = true ...

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.

推荐答案

如果使用 express-session (如果存储为默认值: MemoryStore ),则可以从 req 对象获取存储对象和会话数组:

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"}}' 
}             

passport 道具中的

user 值是您在MongoDB用户集合中的用户的_id.

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

这篇关于尝试查看Express.js Node.js Passport.js中所有已登录的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆