ome*_*arj 18 facebook mongoose node.js passport.js
我正在尝试使用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
));
Run Code Online (Sandbox Code Playgroud)
使用护照初始化和会话
app.use(passport.initialize());
app.use(passport.session());
Run Code Online (Sandbox Code Playgroud)
实际请求处理,请注意我使用的是正确的范围
app.get('/auth/facebook', passport.authenticate('facebook', { scope: ['user_about_me', 'email'] }));
app.get('/auth/facebook/callback', passport.authenticate('facebook', { successRedirect: '/', failureRedirect: '/error' }));
Run Code Online (Sandbox Code Playgroud)
这是我在路由器中的用户创建功能
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);
}
});
}
}
});
};
Run Code Online (Sandbox Code Playgroud)
我确信这是一个新手的错误,但我自己也搞不清楚......
mik*_*iii 39
Facebook返回一些默认属性.如果要访问有关客户端配置文件的更多详细信息,则必须在以下位置声明FacebookStrategy:
passport.use(new FacebookStrategy({
clientID: "...",
clientSecret: "...",
callbackURL: "...",
profileFields: ['id', '...', '...', 'photos', 'emails']
}, ...
Run Code Online (Sandbox Code Playgroud)
因此,一旦您宣布要从Facebook收到的属性,当有人尝试登录您的系统时,他将被要求与您/您的应用分享他的照片或电子邮件.一旦他批准了这个,您就可以访问它的值:
profile.photos[0].value,
profile.emails[0].value,
...
Run Code Online (Sandbox Code Playgroud)
对于电子邮件,有时更改:
passport.authenticate('facebook');
Run Code Online (Sandbox Code Playgroud)
对此:
passport.authenticate('facebook', { scope: 'email'}));
Run Code Online (Sandbox Code Playgroud)
在profileFields中,你应该使用电子邮件(plular)而不是电子邮件(单数):
profileFields: ['id', 'displayName', 'link', 'about_me', 'photos', 'emails']
Run Code Online (Sandbox Code Playgroud)
这在facebook-passport文档README.md中注明:
profileFields参数,它指定字段列表(由Portable Contacts约定命名)
你可以在这里找到passportj的便携式联系人约定
| 归档时间: |
|
| 查看次数: |
12773 次 |
| 最近记录: |