我正在尝试使用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) { …Run Code Online (Sandbox Code Playgroud) 我正在尝试发布/ draft草案的请求,并在我的数据库中创建一个新的"草稿"/更新现有的草稿.之后我想立即重定向到/ draft?id = RELEVANT_ID_HERE页面.
这是我目前的POST请求功能:
app.post('/draft', function(req,res){
var id = req.query.id;
var postTitle = req.body.head;
var article = req.body.article;
if(id){
db.postCatalog.findOneAndUpdate({_id: id}, {title:postTitle, inShort:article.substring(0,100), content:article}, function(err, data){
if (err) {
return handleError(err);
}
else {
console.log(data);
res.status(200).redirect('/draft?id='+id);
}
});
}
else{
id = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
new db.postCatalog({title:postTitle,
_id:id,
author:'temp',
AuthorID:2,
date:'2/3/12',
inShort:article.substring(0,100),
content:article ,
published:false
}).save(function (err, data) {
if (err) { …Run Code Online (Sandbox Code Playgroud) 我登录用户时试图从Facebook获取的一些配置文件字段不会通过.
我在节点中使用passportjs.这是Facebook的策略:
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.get('/auth/facebook', passport.authenticate('facebook', { scope: ['user_about_me', 'email'] }));
Run Code Online (Sandbox Code Playgroud)
结果是"链接","about_me"和"电子邮件"不会被其他字段拉动.