节点js passport-facebook战略认证在帆中

时间:2016-08-19 12:52:18

标签: node.js express sails.js passport-facebook

您好我正在尝试使用Facebook登录验证我的应用。为此,我使用的是passport-facebook。我的代码如下:

//config/passport.js
var passport = require('passport');
var FacebookStrategy = require('passport-facebook').Strategy;
var FACEBOOK_STRATEGY_CONFIG = {
  clientID        : 'xxxxxxxxxxxxxxxxxxxxx',
  clientSecret    : 'xxxxxxxxxxxxxxxxxxxxx',
  callbackURL     :'http://test.com:3000/auth/facebook/callback'
};
function onFacebookStrategyAuth(token, refreshToken, profile, done){

UserCredentials.findOne({'facebook.id' : profile.id}).then(user => {
  if(user)
    return done(null, user);
  else{
    var newUser  = {};
    newUser.facebook.id = profile.id;
    newUser.facebook.token = token;
    newUser.facebook.name = profile.name.givenName + ' ' + profile.name.familyName;
    newUser.facebook.email = profile.emails[0].value;
    return UserCredentials.create(newUser);
  }
}).then(created => done(null,created)).catch(err => done(err, false));

}
passport.use(new FacebookStrategy(FACEBOOK_STRATEGY_CONFIG, onFacebookStrategyAuth));

我的AuthController.js

//controllers/AuthController.js
var passport = require('passport');
module.exports = {
  facebook : function(req,res){
    passport.authenticate('facebook',{scope : 'email'})(req, res);
  },

  facebookCallback : function(req, res){
    passport.authenticate('facebook',{
        successRedirect : '/success',
        failureRedirect : '/fail'
    })(req, res);
  }
};

这是我的config / route.js

module.exports.routes = {
    '/auth/facebook': 'AuthController.facebook',
    '/auth/facebook/callback':  'AuthController.facebookCallback',
    '/success': {view : 'success'},
    '/fail': {view : 'fail' }
  }

当我点击提交按钮时,我的提交按钮操作被定义为' / auth / facebook'。但是我在控制台上收到以下错误:

/node_modules/passport/lib/middleware/authenticate.js:343
        next(err);
        ^
TypeError: next is not a function
    at Strategy.strategy.error (/home/harsh/Desktop/cmbackend/node_modules/passport/lib/middleware/authenticate.js:343:9)
    at /home/harsh/Desktop/cmbackend/node_modules/passport-oauth2/lib/strategy.js:166:34
    at /home/harsh/Desktop/cmbackend/node_modules/oauth/lib/oauth2.js:177:18
    at passBackControl (/home/harsh/Desktop/cmbackend/node_modules/oauth/lib/oauth2.js:123:9)
    at IncomingMessage.<anonymous> (/home/harsh/Desktop/cmbackend/node_modules/oauth/lib/oauth2.js:143:7)
    at emitNone (events.js:72:20)
    at IncomingMessage.emit (events.js:166:7)
    at endReadableNT (_stream_readable.js:905:12)
    at nextTickCallbackWith2Args (node.js:441:9)
    at process._tickDomainCallback (node.js:396:17)

1 个答案:

答案 0 :(得分:1)

错误暗示你发生了什么,护照fns是中间件,他们希望在next收到(req, res, next)回调,所以你有两个选择。

将中间件直接分配给控制器操作,如下所示:

var passport = require('passport');
module.exports = {
    facebook : passport.authenticate('facebook',{scope : 'email'}),

    facebookCallback :passport.authenticate('facebook',
      {successRedirect : '/success', failureRedirect : '/fail'}
    )
};

或者像这样,如果你需要在动作上做其他事情(或者只是为了更清楚地了解这里发生的事情)

var passport = require('passport');
module.exports = {
  facebook : function(req,res, next){
    // do something else here
    passport.authenticate('facebook',{scope : 'email'})(req, res, next);
  },

  facebookCallback : function(req, res){
    // You can do something else here
    passport.authenticate('facebook',{
      successRedirect : '/success',
      failureRedirect : '/fail'
    })(req, res, next);
  }
};

总而言之,你需要在fn passport.authenticate返回

旁边传递