简体   繁体   English

Passport.js,具有相同凭据的Express会话跨浏览器

[英]Passport.js, Express session Cross-browser with same credentials

I've written code with Passport.js for authentication purpose. 我已经使用Passport.js编写了用于身份验证的代码。 While user logged into chrome and using same credentials user logged into another browser 'FF'. 当用户登录chrome并使用相同的凭据时,用户登录了另一个浏览器“ FF”。

As we all know that Passport.js store all details into req.users and req.session.passport.users . 众所周知, Passport.js将所有详细信息存储到req.usersreq.session.passport.users If from one browser user update some details how can we update into another browsers req object without logout? 如果从一个浏览器用户更新一些细节,我们如何在不注销的情况下更新到另一个浏览器req对象?

Same kind of, If admin update user1 details and he already logged in than how that will affect? 同理,如果管理员更新user1的详细信息并且他已经登录,那将如何影响?

Any clue? 有什么线索吗?

As we all know that Passport.js store all details into req.users and 众所周知, Passport.js将所有详细信息存储到req.users

Not necessarily. 不必要。 passport.js does not store user details in req.user , but your passport.js integration code loads the user details from some backend storage and then puts it in the request object on every request. passport.js不会在req.user store用户详细信息,但是您的passport.js集成代码将从某个后端存储中loads用户详细信息,然后将其放在每个请求的请求对象中。

So it is up to you to update the user in the backend and decide when to retrieve a new version ( instead of just deserializing jwt , for example ) on every request just as well. 因此,还取决于您是否在后端更新用户并决定何时针对每个请求检索新版本(例如,不只是反序列化jwt )。

Sample code from http://www.passportjs.org/docs/basic-digest/ 来自http://www.passportjs.org/docs/basic-digest/的示例代码

passport.use(new BasicStrategy(
  function(username, password, done) {
    User.findOne({ username: username }, function (err, user) {
      if (err) { return done(err); }
      if (!user) { return done(null, false); }
      if (!user.validPassword(password)) { return done(null, false); }
      return done(null, user);
    });
  }
));

This code is executed on every single request which means that on every request to the server your user is loaded from your database. 该代码在每个单个请求上执行,这意味着在对服务器的每个请求上,都会从数据库中加载用户。

Even if you're working with multiple sessions in multiple browsers the result is the same. 即使您在多个浏览器中使用多个会话,结果也相同。 So it is up to you to handle when and how you want to update your user in your database. 因此,由您决定何时以及如何更新数据库中的用户。

Otherwise if you don't load your user from an external datasource but eg deserialize the whole user object from jwt ( which is not recommended unless you really understand what you're doing ) then you need to think of a synchronisation strategy eg check some updated flag in db or some cache on deserialization 否则,如果您不从外部数据源加载用户,而是例如从jwt反序列化整个用户对象(除非您真的了解自己的工作,否则不建议这样做),那么您需要考虑一种同步策略,例如检查一些updated db标志或反序列化时的某些缓存

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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