node.js - 尝试理解 Node JS 路由代码

标签 node.js express

我试图理解one实现基于 SAML 的 SSO 的开源实现,我无法理解 this 中的以下快速路由器方法类:

router.get('/', function(req, res, next) {
  console.log(arguments);
    if(req.isAuthenticated()){
        res.render('index', {
            title: 'sp1 - My Application',
            user: req.user
        });
    }else{
      console.log('not authentcated sending to authenticate');
        res.redirect('/login');
    }
});

我的问题是:

where exactly the code is setting `isAuthenticated` flag to true or false?

当我第一次启动/login 时,我发现它是 false,但是当我从 idp(身份提供商)获得重定向时,此标志为 true,并且我将进入 if 条件。

最佳答案

这个方法确实来自passport认证系统,

您可以检查函数本身 here :

/**
* Test if request is authenticated.
*
* @return {Boolean}
* @api public
*/
req.isAuthenticated = function() {
  var property = 'user';
  if (this._passport && this._passport.instance._userProperty) {
 property = this._passport.instance._userProperty;
}

 return (this[property]) ? true : false;
};

关于node.js - 尝试理解 Node JS 路由代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43310427/

相关文章:

node.js - express 中间件测试 mocha chai

node.js - 在做其他事情之前,如何等待循环中的 promise 完成?

node.js - 如何与 Socket.IO 1.x 和 Express 4.x 共享 session ?

javascript - Express js 4x :req. 参数返回空对象

javascript - kriskowal/q node.js q.all 和传播

javascript - 转到下一页并在nightmareJS中抓取链接

javascript - 异步/等待操作发生在预期的顺序之外

node.js - 防止自动重定向并获取 PassportJs "authenticate"方法的重定向 URL

node.js - Sequelize 似乎限制在 30 个模型

javascript - 为什么 onmessage 监听器不处理初始 SSE 事件?