2016-08-18 107 views
1

我已經實現了passport.js登錄時,我把我的passport.authenticated()函數在中間件。isAuthenticated返回函數false

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

你必須設置的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);