isAuthenticated返回函数为false

我已经实现了passport.jslogin时,我把我的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); 

login成功。

当我通过使用isAuthenticate()函数validation其他请求时,它返回false。

如果我从passport.autheated中删除了中间件函数,那么其他请求将返回true。 但我需要中间件function,因为用户未经过身份validation时返回自定义响应。 请任何一个帮我,我有什么实施错误。

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

 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);