2016-11-07 93 views
1

我正在使用Ionic2前端和Node/Express/Mongo后端。我配置了Passport本地策略来登录用户,并配置了一个简单的中间件来确定用户是否通过身份验证来访问受限制的终端。当我在Postman客户端中测试时,这一切都正常。但是,当我从浏览器登录并尝试访问端点时失败。所看到的是浏览器(Chrome)在“set-cookie”标题中返回cookie值,但Passport似乎在“Cookie”标题中查找cookie值。我尝试从我的应用程序的GET请求中设置Cookie标头,但显然这是出于安全原因无法完成的。我怎样才能让应用程序返回Cookie头中的cookie值?无法使用护照本地策略对用户进行身份验证

Middleware 
function userAuthenticated(req, res, next){ 
    //returns value from Postman; undefined from app  
    console.log(req.get('Cookie').toString()); 
    //returns value from app; undefined from Postman 
    console.log(req.get('set-cookie').toString()); 

    if(req.isAuthenticated()){ 
     return next(); 
    } 
     res.send([{"message":"not logged in"}]); 
} 

Protected endpoint 
router.get('/books', userAuthenticated, function (req, res, next) { 
    Book.find({}, function(err, docs){ 
     if(err) { 
      res.send(err); 
     } else { 
      res.json(docs); 
      //next(); 
     } 
    }) 
}); 

回答

相关问题