如果没有在帖子请求中登录,则重定向到登录页面

时间:2018-01-16 06:29:40

标签: javascript node.js

app.post("/customer/goods/available/disableGoods", auth.restrict,
        auth.acl([__FEATUREENUM.FP_SALE_GOODS]),
        postDisableGoodsHandler);}

exports.restrict = function (req, res, next) {
    logger.enter();
    if (req.session.operator) {
    if (req.url.indexOf("customer") > -1 && 
      req.session.operator.operatorType == "CUSTOMER") {
      return next();
    }
    if (req.url.indexOf("customer") > -1 && 
      req.session.operator.operatorType != "CUSTOMER") {
      logger.debug("您所访问的页面"+req.url+"没有权限");
      return res.redirect("/no_permission")
    }
    if (req.url.indexOf("customer") == -1 && 
    req.session.operator.operatorType == "CLIENT") {
      return next();
    }}
    logger.ndump("url", req.url);var nextTo = 
    encodeURIComponent(req.url);res.redirect("/loginNew?nextTo=" + 
    nextTo);};


    app.post("/loginNew", getLoginHandler);


    function getLoginHandler(req, res) {
    logger.enter();

    var nextTo = req.param('nextTo');
    logger.ndump("nextTo", nextTo);
    if (!nextTo)
      nextTo = APPURL;

    nextTo = encodeURIComponent(nextTo);

    dataService.commonData(req, function (data) {
      data = underscore.extend(data, {passHolder: "", nextTo: nextTo});
      data['paginator'] = {};
      res.render("pages/common/login.ejs", {data: data});
    });
  }

This is the image

因为这些,我想使用中间件身份验证。重新绑定以检查状态是否在发布请求中登录,否则,重定向到登录页面。我得到了回复,但浏览器并没有像我一样跳到新页面。

1 个答案:

答案 0 :(得分:0)

首先,您不应该两次声明var nextTo。 也许您应该尝试制作会话数据,如req.session.loggedIn,然后检查。 像:

if(!req.session.loggedIn){
    encodeURIComponent(req.url);res.redirect("/loginNew?nextTo=" + 
    nextTo);};
}