【问题标题】:Sails JS passport authentication making policy layer useless?Sails JS 护照身份验证使策略层无用?
【发布时间】:2014-11-04 08:01:37
【问题描述】:

我是 SailsJS 的新手,我想征求专家意见,我的实现是否正常。在 SailsJS 的 passport authentication package 的帮助下,我能够进行某种身份验证。

/controller/members.js:

module.exports = { 
  //deny all non authenticated users from accessing this controller
  '*':function(req,res,next){
    if(!req.user){
      res.view(403);
    }else{
      next();
    }
  },
  //once user is authenticated through passport, it is redirected here
  'welcome':function(req,res){
    res.view();
  },
  //when user clicks logout
  'logout':function(req,res){
   req.logout();
   res.redirect("/login");
  },
};

但是,正如您从中看到的,输入策略是在控制器本身内编码的。我根本没有使用策略。那么有了包的帮助,SailsJS 中的策略层就完全没用了?这是实施passportJS的正确方法吗?如果我错了,请纠正我,我想我绝对是。

【问题讨论】:

    标签: sails.js passport.js


    【解决方案1】:

    您应该能够改为使用策略来实现上述内容。如果您创建一个名为 isAuthenticated.js 的策略,您可以将您的规则从控制器移到其中:

    module.exports = function(req, res, next) {
      if(!req.user){
        res.view(403);
      } else {
        next();
      }
    };
    

    (并从您的控制器中删除“*”方法)。然后,您可以在 config/policies.js 文件中将“isAuthenticated”指定为策略,并从那里控制访问:

    module.exports.policies = {
      members: {
        '*':      'isAuthenticated',
        'welcome': true,
        'logout':  true
      }
    }
    

    【讨论】:

      【解决方案2】:

      要在使用护照实现身份验证时使用策略,您必须将它们配置为快速中间件(在 config/express.js 中),而不是将它们添加为控制器。

      看看这个项目,看看它是如何完成的:

      Sails Social Auth

      【讨论】:

      • 我想使用 kasperisager 提供的软件包,因为它具有将所有登录方法(通过社交)捆绑在一起并保留单个用户 ID 的“魔法”。
      • 对不起。不知道这个特定的 npm 包。为什么不尝试在 Github 存储库中提出问题?
      猜你喜欢
      • 2016-12-26
      • 2015-09-10
      • 1970-01-01
      • 1970-01-01
      • 2020-04-20
      • 1970-01-01
      • 2017-03-21
      • 2020-10-21
      • 2018-01-28
      相关资源
      最近更新 更多