在node.js中对护照进行反序列化时,不仅要添加req.user

时间:2014-03-09 00:41:20

标签: node.js express passport.js

Passport restores the user object retrieved from database to req.user in its .deserializeUser method.

我想知道我是否可以通过相同的req.cart方法获得与用户相关但存储在不同数据库中的req.inventory.deserializeUser等其他内容?

1 个答案:

答案 0 :(得分:1)

您可以为此定义自定义中间件。演示代码:

// ...
app.use(passport.initialize());
app.use(passport.session());

app.use(function(req, res, next) {
  // the use logged in.
  if (req.isAuthenticated()) {
    // Fetch the user information from other databases by `req.user.id`.
    // Store the returned information as `req.cart` and `req.inventory` separately.
  };

  next();
});