【问题标题】:Unable to authenticate user with passport local strategy无法使用护照本地策略对用户进行身份验证
【发布时间】:2017-03-21 08:37:19
【问题描述】:

我正在使用 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();
       }
   })  
});

【问题讨论】:

    标签: express cookies passport.js middleware setcookie


    【解决方案1】:
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-20
    • 1970-01-01
    • 2021-08-18
    • 2018-11-24
    • 2017-08-12
    • 2015-09-10
    • 2018-01-28
    相关资源
    最近更新 更多