创建新BearerStrategy时出错

时间:2013-07-09 14:28:57

标签: node.js express passport.js

我正在尝试使用OAuth2orize,并且API端点使用BearerStrategy,如Git中example中所述。代码如下

passport.use(new BearerStrategy(
  function(accessToken, done){
    accessTokens.find(accesstoken, function(err, token){
      if (err) {return done(err);}
      if (!token) {return done(null, false); }

      users.find(token.user_id, function(err, user){
        if (err) { return done(err); }
        if (!user) { return done(null, false); }

        var info = { scope: '*' }
        done(null, user, info);
      });
    });
  }
));

我尝试启动时遇到以下错误:

/Users/saransh2012/Developer/vypics/node_modules/passport/lib/passport/index.js:51
  if (!name) throw new Error('authentication strategies must have a name');
                   ^
Error: authentication strategies must have a name
    at Passport.use (/Users/saransh2012/Developer/vypics/node_modules/passport/lib/passport/index.js:51:20)
    at Object.<anonymous> (/Users/saransh2012/Developer/vypics/android_app/auth.js:54:10)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/Users/saransh2012/Developer/vypics/android_app/app.js:11:1)
    at Module._compile (module.js:456:26)

在同一个文件中,我也在使用New BasicStrategy,但这仍然有效,但仍然没有用。

1 个答案:

答案 0 :(得分:0)

您需要将空对象作为第一个参数传递给BearerStrategy的构造函数。即

new BearerStrategy(
  { }, // <-- the first argument should be an empty object
  function(accessToken, done){
    accessTokens.find(accesstoken, function(err, token){
      if (err) {return done(err);}
      if (!token) {return done(null, false); }

      users.find(token.user_id, function(err, user){
        if (err) { return done(err); }
        if (!user) { return done(null, false); }

        var info = { scope: '*' }
        done(null, user, info);
      });
    });
  }
)