我正在尝试每次用户登录时找到更新方式:“profile._json.friends.data”(因为它只会在第一次登录时选择并保存数据库中的朋友列表)。 / p>
我听说我们可以使用:
findOneAndUpdate 或Facebook Graph API(以避免更新)
但是我不确定我该怎么做或者我是否以正确的方式开始......
我正在使用passport-facebook,mongodb / mongoose
我希望这是可以理解的,如果你有更好的方法,请不要犹豫!
提前谢谢!
// Passport configuration =====================================================
// used to serialize the user for the session
passport.serializeUser(function(user, done) {
done(null, user.id);
});
// used to deserialize the user
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
app.use(function(req, res, next){
res.locals.currentUser = req.user;
next();
});
// =========================================================================
// FACEBOOK ================================================================
// =========================================================================
passport.use(new FacebookStrategy({
// pull in our app id and secret from our auth.js file
clientID : configAuth.facebookAuth.clientID,
clientSecret : configAuth.facebookAuth.clientSecret,
callbackURL : configAuth.facebookAuth.callbackURL,
profileFields : configAuth.facebookAuth.profileFields
},
// facebook will send back the token and profile
function(token, refreshToken, profile, done) {
// asynchronous
process.nextTick(function() {
// find the user in the database based on their facebook id
User.findOne({ 'facebook.id' : profile.id }, function(err, user) {
// if there is an error, stop everything and return that
// ie an error connecting to the database
if (err)
return done(err);
// if the user is found, then log them in
if (user) {
return done(null, user); // user found, return that user
} else {
// if there is no user found with that facebook id, create them
var newUser = new User();
// set all of the facebook information in our user model
newUser.facebook.id = profile.id; // set the users facebook id
newUser.facebook.token = token; // we will save the token that facebook provides to the user
newUser.facebook.name = profile.displayName; // pull name from profile displayName
profile.emails ? newUser.facebook.email = profile.emails[0].value : newUser.facebook.email = "no email"; // if email isn't released from fb then set to "no email"
profile.photos ? newUser.facebook.picture = profile.photos[0].value : '/img/faces/unknown-user-pic.jpg';
newUser.facebook.gender = profile.gender;
newUser.facebook.friends = profile._json.friends.data;
// save our user to the database
newUser.save(function(err) {
if (err)
throw err;
// if successful, return the new user
return done(null, newUser);
});
}
});
});
}));
app.get('/profile', isLoggedIn, function(req, res) {
res.render('profile.ejs', {
user : req.user // get the user out of session and pass to template
});
});
// =====================================
// HOME PAGE ===========================
// =====================================
app.get('/', function(req, res) {
res.render('landing'); // load the index.ejs file
});
// route for showing the profile page
app.get('/profile', isLoggedIn, function(req, res) {
res.render('profile', {
user : req.user // get the user out of session and pass to template
});
});
// =====================================
// FACEBOOK ROUTES =====================
// =====================================
// route for facebook authentication and login
app.get('/auth/facebook', passport.authenticate('facebook', { authType: 'rerequest', scope : ['email', 'user_friends'] }));
// handle the callback after facebook has authenticated the user
app.get('/auth/facebook/callback',
passport.authenticate('facebook', {
successRedirect : '/profile',
failureRedirect : '/'
}));
// route for logging out
app.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});