req.isAuthenticated将无法正常工作

时间:2018-02-16 19:59:30

标签: javascript node.js passport.js

我使用Node Js,Express,Mongoose和Passport.js构建了一个迷你CMS系统。 当客户端注销时,我在控制台中收到此错误:'Cannot read property 'id' of null',错误来自PrivateThread.find().lean().then()...

// Pass User Data To Template
function sendUserData(req, res, next) {
  if (req.isAuthenticated()) {
    User.findById(req.user.id, doNotSend).then(user => {
      switch(true) {
        case user.banned:
          req.logOut();
          flashRedirect(req, res, '/login', 'res-err', 'Some message');
          break;

        case !user.activated:
          req.logOut();
          flashRedirect(req, res, '/login', 'res-err', 'Somem message');
          break;

        default:
          res.locals.foundUser = user;
          PrivateThread.find({ participants: req.user.id }).lean().then(foundThreads => {
            let notifications = foundThreads.filter(thread => !thread.readBy.toString().includes(req.user.id));
            res.locals.notifications = notifications.length;
          });
          next();
      }
    })
    .catch((err) => res.send(err));
  } else {
    next();
  }
}

这是退出路线:

router.get('/logout', (req, res) => {
  req.logOut();
  res.redirect('/');
});

我只是在客户端通过注销页面注销时才收到错误。

0 个答案:

没有答案