2016-08-19 88 views
1

你好我試圖使用Facebook登錄認證我的應用程序。爲此我使用passport-facebook。我的代碼如下給予:節點js 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); 
    } 
}; 

這裏是我的配置/ 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

的錯誤提示,你到底是怎麼回事,護照FNS是中間件,他們希望收到(req, res, next)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返回