我正在尝试使用passportjs登录使用Facebook服务。网上有很多例子,但没有一个真正明确地说明没有持久会话的实现,因为我计划使用JWT来识别我的用户。
我已经尝试过这个,但我一直收到错误回复Cannot GET /auth/facebook/callback
。请参阅下面的实施。
路由文件
router.get('/facebook', passport.authenticate('facebook',{ scope : ['email'] }));
router.get('/facebook/callback',
passport.authenticate('facebook', {
session: false
}));
Passportjs
passport.use(new FacebookStrategy({
clientID: config.facebookAppId,
clientSecret: config.facebookAppSecret,
callbackURL: config.facebookCallbackURL,
profileFields: ['email']
},
function(accessToken, refreshToken, profile, done) {
//find the user based off of the facebook profile id
User.findOne({oauthID: profile.id}).exec()
//find the user or create a new record
.then(function(user){
if(user) return done(null, user);
var user = new User({
oauthID: profile.id,
oauthProvider: 'facebook',
email: profile.emails[0].value,
name: profile.displayName
});
user.save();
})
//send back error if encountered
.catch(done);
}
));