Javascript 将passport.js与yeoman和grunt一起使用进行身份验证

Javascript 将passport.js与yeoman和grunt一起使用进行身份验证,javascript,node.js,gruntjs,yeoman,passport.js,Javascript,Node.js,Gruntjs,Yeoman,Passport.js,我正在研究如何将passport.js与grunt/yeoman一起使用。我有以下资料: // at the top of my gruntfile.js var passport = require('passport'); var BasicStrategy = require('passport-http').BasicStrategy; passport.use(new BasicStrategy( function(username, password, done) {

我正在研究如何将passport.js与grunt/yeoman一起使用。我有以下资料:

// at the top of my gruntfile.js
var passport = require('passport');
var BasicStrategy = require('passport-http').BasicStrategy;

passport.use(new BasicStrategy(
  function(username, password, done) {
    return done(null, true); // I would expect this to always succeed, but still challenge for credentials
  }
));

// further down in my connect config.
livereload: {
    options: {
        middleware: function (connect) {
            return [
                lrSnippet,
                passport.initialize(),
                passport.authenticate('basic', { session: false }),
                mountFolder(connect, '.tmp'),
                mountFolder(connect, yeomanConfig.app)
            ];
        }
    }
}
对于每个请求,响应只包含
未经授权的
。删除对
passport.authenticate的调用可以使页面正常工作,但显然现在没有身份验证。我已经尝试过改变中间件的顺序,但没有任何帮助,而且我远不是yeoman/grunt的专家,所以我不完全确定还有什么可以尝试


非常感谢您的帮助。

我想您需要在
基本策略
回调中向
done()
传递一个对象。我记得,passport JS使用此对象在express应用程序中填充req.user,因此我认为它可能需要一个
对象
而不是
布尔值

下面是我在许多应用程序中使用的同一功能的一个更健壮的示例:

  passport.use(new BasicStrategy(
    function(clientID, clientSecret, done) {
      AuthClient.findOne({ clientID: clientID }, function(err, client) {
        if (err) { return done(err); }
        if (!client) { return done(null, false); }
        if (client.secret != clientSecret) { return done(null, false); }
        return done(null, client);
      });
    }
  ));

如您所见,基本策略用于分析与用户名/密码组合等效的clientID和clientSecret。由于您实际上并没有像我的示例中所示那样从数据库中提取它,我希望您只需按照上面的建议将
{}
传递到
done(null,{})
,它可能会更好。

在尝试授权时是否发送授权标头?@vadim yes,当不通过passport时,这一切都有效。扫描源代码,我几乎可以肯定,用户对象是什么并不重要。代码仅验证以确保其真实性,然后将
req.user
设置为您传递的任何内容。