我正在尝试使用passport-facebook为我的应用程序建立登录系统.一切顺利,除了从请求中返回未定义的2个字段.
我将发布我的整个代码用于登录程序,因为我在这里没有看到很多关于它的信息,即使在这个问题上有很多问题.
这是app.js中的配置
var passport = require('passport'); var FacebookStrategy = require('passport-facebook').Strategy; passport.serializeUser(function(user, done) { done(null, user.facebookId); }); passport.deserializeUser(function(id, done) { routes.findUserById(id, function(err, user) { done(err, user); }); }); passport.use(new FacebookStrategy({ clientID: FACEBOOK_APP_ID, clientSecret: FACEBOOK_APP_SECRET, callbackURL: FACEBOOK_CALLBACK_URL, profileFields: ['id', 'displayName', 'link', 'about_me', 'photos', 'email'] }, routes.handleLogin ));
使用护照初始化和会话
app.use(passport.initialize()); app.use(passport.session());
实际请求处理,请注意我使用的是正确的范围
app.get('/auth/facebook', passport.authenticate('facebook', { scope: ['user_about_me', 'email'] })); app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect: '/', failureRedirect: '/error' }));
这是我在路由器中的用户创建功能
exports.handleLogin = function(accessToken, refreshToken, profile, done) { db.userCatalog.findOne({ facebookId: profile.id }, function(err, existingUser) { if (err) { return done(err); } else { if(existingUser) { console.log('User: ' + existingUser.name + ' found and logged in!'); done(null, existingUser); } else { new db.userCatalog({ name: profile.displayName, facebookId: profile.id, link: profile.link, picture: profile.photos[0].value, bio: profile.about_me, email: profile.email }).save(function (err, data) { if (err) { return done(err); } else { console.log('New user: ' + data + ' created and logged in!'); done(null, data); } }); } } }); };
以及完成登录过程后创建新用户时的结果:
我确信这是一个新手的错误,但我自己也搞不清楚......
在profileFields中,你应该使用电子邮件(plular)而不是电子邮件(单数):
profileFields: ['id', 'displayName', 'link', 'about_me', 'photos', 'emails']
这在facebook-passport文档README.md中注明:
profileFields参数,它指定字段列表(由Portable Contacts约定命名)
你可以在这里找到passportj的便携式联系人约定
Facebook返回一些默认属性.如果要访问有关客户端配置文件的更多详细信息,则必须在以下位置声明FacebookStrategy
:
passport.use(new FacebookStrategy({ clientID: "...", clientSecret: "...", callbackURL: "...", profileFields: ['id', '...', '...', 'photos', 'emails'] }, ...
因此,一旦您宣布要从Facebook收到的属性,当有人尝试登录您的系统时,他将被要求与您/您的应用分享他的照片或电子邮件.一旦他批准了这个,您就可以访问它的值:
profile.photos[0].value, profile.emails[0].value, ...
对于电子邮件,有时更改:
passport.authenticate('facebook');
对此:
passport.authenticate('facebook', { scope: 'email'}));