路由处理中的旁路认证

时间:2017-11-27 14:41:35

标签: node.js express authentication

我已经设置了一个带有注册主页和一些需要登录的内部页面的应用程序。 我使用Node和Express.js来设置服务器并控制路由和身份验证工作正常:如果我尝试访问localhost:port / clientPage,如果我以前登录过,我会得到所需的页面,否则会收到错误消息。

问题是,如果我尝试访问localhost:port / clientPage。 html 即使我没有活动会话,我也会得到clientPage。在这种情况下,如何确保之前描述的相同 - 期望 - 行为?我将我的GET路由的代码附加到clientPage:

router.get('/clientPage', function (req, res, next) {
  User.findById(req.session.userId)
    .exec(function (error, user) {
      if (error) {
        return next(error);
      } else {
        if (user === null) {
          var err = new Error('Not authorized! Go back!');
          err.status = 400;
          return next(err);
        } else {
          return res.sendFile(path.join(__dirname + '/../views/clientPage.html'));
        }
      }
    });
});

0 个答案:

没有答案