isAuthenticated return 函数错误

isAuthenticated return function false

我在登录时实现了 passport.js,我将 passport.authenticated() 函数放在中间件中。

app.post('/api/v1/login',
        function (req, res, next) {
            passport.authenticate('local-login', function (err, user, info) {
                if (user == false) {
                    return res.json(ApiException.newNotAllowedError(api_errors.invalid_auth_credentials.error_code, null).addDetails(api_errors.invalid_auth_credentials.description));
                }
                else {
                    next();
                }
            })(req, res, next);
        }, controllerIndex.auth.login);

登录成功

当我使用 isAuthenticate() 函数验证其他请求时,它 return false。

如果我从 passport.authenticated 中删除中间件功能,则其他请求 return 为真。但我需要中间件功能,因为在用户未通过身份验证时 return 自定义响应。请任何人帮助我我实施错误的地方。

您必须设置 cookie 或确保用户使用 req.login

登录
app.post('/api/v1/login',
  function (req, res, next) {
   passport.authenticate('local-login', function (err, user, info) {
     if (user == false) {
        return res.json(ApiException.newNotAllowedError(api_errors.invalid_auth_credentials.error_code, null).addDetails(api_errors.invalid_auth_credentials.description));
     }else{

   req.logIn(user, function(err) {
      if (err) { return next(err); }
      next();
    });

   }
})(req, res, next);
}, controllerIndex.auth.login);