抛出新的TypeError('OAuth2Strategy需要验证回调');

时间:2018-11-18 03:16:11

标签: node.js express oauth-2.0 facebook-authentication

所以我正在学习有关node.js和facebook身份验证的信息,并且遇到了此错误:

/home/lindan4/Git/HelloNodeJS/ChatTime/node_modules/passport-oauth2/lib/strategy.js:79
  if (!verify) { throw new TypeError('OAuth2Strategy requires a verify callback'); }
                 ^

TypeError: OAuth2Strategy requires a verify callback
    at Strategy.OAuth2Strategy (/home/lindan4/Git/HelloNodeJS/ChatTime/node_modules/passport-oauth2/lib/strategy.js:79:24)
    at new Strategy (/home/lindan4/Git/HelloNodeJS/ChatTime/node_modules/passport-facebook/lib/strategy.js:55:18)
    at module.exports (/home/lindan4/Git/HelloNodeJS/ChatTime/app/auth/index.js:42:16)
    at Object.<anonymous> (/home/lindan4/Git/HelloNodeJS/ChatTime/server.js:3:22)
    at Module._compile (internal/modules/cjs/loader.js:707:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:718:10)
    at Module.load (internal/modules/cjs/loader.js:605:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:544:12)
    at Function.Module._load (internal/modules/cjs/loader.js:536:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:760:12)
    at startup (internal/bootstrap/node.js:308:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:878:3)

代码如下:

'use strict';

const passport = require('passport');
const config = require('../config');
const FacebookStrategy = require('passport-facebook').Strategy;

const helper = require('../helpers');

module.exports = () => {

  passport.serializeUser((user, done) => {
    done(null, user.id);
  });


  passport.deserializeUser((id, done) => {
    helper.findById(id).then(user => {
      done(null, user);
    }).catch('Error deserializing user');
  });

  //Issue is here
  let authProcessor = (accessToken, refreshToken, profile, done) => {
      //Find a user in the local MongoDB instance using the profile.id


      helper.findRecord(profile.id).then(result => {
        if (result)
        {
          done(null, result);
        }
        else
        {
            //Create a new user and return
            helper.createNewUser(profile).then(newChatUser => {
              done(null, newChatUser)
            }).catch(error => console.log('Error when creating new user'));
        }
      });
  }

  passport.use(new FacebookStrategy(config.fb), authProcessor);
}

错误在于此行:

 passport.use(new FacebookStrategy(config.fb), authProcessor);

我看过Google和Stack Overflow,还没有找到解决方案。我希望有人可以帮助我。供您参考,“ config”模块的实现如下所示:

'use strict';

const devEnv = require('./development.json');

if (process.env.NODE_ENV === 'production')
{
  //Offer production stage environment variables
  module.exports =
  {
    host: process.env.host || "",
    dbURI: process.env.dbURI,
    sessionSecret: process.env.sessionSecret,
    fb: {
      clientID: process.env.fbClientID,
      clientSecret: process.env.fbClientSecret,
      callbackURL: process.env.host + "auth/facebook/callback",
      profileFields: ['id', 'displayName', 'photos']
    }
  }
}
else
{
  //Offer dev stage and data
  module.exports = {
    host: devEnv.host,
    dbURI: devEnv.dbURI,
    sessionSecret: devEnv.sessionSecret
  }
}

2 个答案:

答案 0 :(得分:3)

您这样称呼它:

passport.use(new FacebookStrategy({}), function(){} )

但是应该是:

passport.use(new FacebookStrategy( {}, function(){} ) ) //Note where I moved the parenthesis

FacebookStrategy带有2个参数,第一个是具有id / secret / url的对象,第二个是函数,new FacebookStrategy是唯一传递给passportjs.use()的东西

答案 1 :(得分:0)

根据passport.js文档,在配置facebook策略函数passport.use()时,其中第二个参数即函数return the callback function应该尚未返回。