首页 文章

isAuthenticated返回函数false

提问于
浏览
1

我在登录时实现了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 回答

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

相关问题