isAuthenticated返回函数false

时间:2016-08-18 07:55:16

标签: json node.js passport.js

我已经实现了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()函数验证其他请求时,它返回false。

如果我从passport.authenticated中删除了中间件函数,则其他请求返回true。但我需要中间件功能,因为在用户未经过身份验证时返回自定义响应。请任何人帮我解决我的错误。

1 个答案:

答案 0 :(得分:1)

您必须设置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);
相关问题