Update profile picture in Facebook strategy

This commit is contained in:
Sahat Yalkabov
2014-01-31 22:23:19 -05:00
parent e177ac10de
commit 90c5278bbc

View File

@ -34,21 +34,21 @@ passport.use(new LocalStrategy({ usernameField: 'email' }, function(email, passw
})); }));
/** /**
* Sign in with Facebook. * Sign in with Facebook.
* *
* Possible authentication states: * Possible authentication states:
* *
* 1. User is logged in. * 1. User is logged in.
* a. Already signed in with Facebook before. (MERGE ACCOUNTS, EXISTING ACCOUNT HAS PRECEDENCE) * a. Already signed in with Facebook before. (MERGE ACCOUNTS, EXISTING ACCOUNT HAS PRECEDENCE)
* b. First time signing in with Facebook. (ADD FACEBOOK ID TO EXISTING USER) * b. First time signing in with Facebook. (ADD FACEBOOK ID TO EXISTING USER)
* 2. User is not logged in. * 2. User is not logged in.
* a. Already signed with Facebook before. (LOGIN) * a. Already signed with Facebook before. (LOGIN)
* b. First time signing in with Facebook. (CREATE ACCOUNT) * b. First time signing in with Facebook. (CREATE ACCOUNT)
*/ */
passport.use(new FacebookStrategy(secrets.facebook, function (req, accessToken, refreshToken, profile, done) { passport.use(new FacebookStrategy(secrets.facebook, function (req, accessToken, refreshToken, profile, done) {
if (req.user) { if (req.user) {
User.findOne({ facebook: profile.id }, function(err, existingUser) { User.findOne({ $or: [{ facebook: profile.id }, { email: profile.email }] }, function(err, existingUser) {
if (existingUser) { if (existingUser) {
existingUser.github = existingUser.github || req.user.github; existingUser.github = existingUser.github || req.user.github;
existingUser.google = existingUser.google || req.user.google; existingUser.google = existingUser.google || req.user.google;
@ -59,7 +59,7 @@ passport.use(new FacebookStrategy(secrets.facebook, function (req, accessToken,
existingUser.tokens = _.union(existingUser.tokens, req.user.tokens); existingUser.tokens = _.union(existingUser.tokens, req.user.tokens);
existingUser.save(function(err) { existingUser.save(function(err) {
User.remove({ _id: req.user.id }, function(err) { User.remove({ _id: req.user.id }, function(err) {
req.flash('info', { msg: 'Your accont has been merged with an existing one.' }); req.flash('info', { msg: 'Your account has been merged with an existing one that belongs to you' });
return done(null, existingUser); return done(null, existingUser);
}); });
}); });
@ -69,7 +69,7 @@ passport.use(new FacebookStrategy(secrets.facebook, function (req, accessToken,
user.tokens.push({ kind: 'facebook', accessToken: accessToken }); user.tokens.push({ kind: 'facebook', accessToken: accessToken });
user.profile.name = user.profile.name || profile.displayName; user.profile.name = user.profile.name || profile.displayName;
user.profile.gender = user.profile.gender || profile._json.gender; user.profile.gender = user.profile.gender || profile._json.gender;
user.profile.picture = user.profile.picture || profile._json.profile_image_url; user.profile.picture = user.profile.picture || 'https://graph.facebook.com/' + profile.id + '/picture?type=large';
user.save(function(err) { user.save(function(err) {
done(err, user); done(err, user);
}); });
@ -78,7 +78,7 @@ passport.use(new FacebookStrategy(secrets.facebook, function (req, accessToken,
}); });
} else { } else {
User.findOne({ facebook: profile.id }, function(err, existingUser) { User.findOne({ facebook: profile.id }, function(err, existingUser) {
console.log(profile); console.log(profile)
if (existingUser) return done(null, existingUser); if (existingUser) return done(null, existingUser);
var user = new User(); var user = new User();
user.email = profile._json.email; user.email = profile._json.email;
@ -86,7 +86,7 @@ passport.use(new FacebookStrategy(secrets.facebook, function (req, accessToken,
user.tokens.push({ kind: 'facebook', accessToken: accessToken }); user.tokens.push({ kind: 'facebook', accessToken: accessToken });
user.profile.name = profile.displayName; user.profile.name = profile.displayName;
user.profile.gender = profile._json.gender; user.profile.gender = profile._json.gender;
user.profile.picture = profile._json.profile_image_url; user.profile.picture = 'https://graph.facebook.com/' + profile.id + '/picture?type=large';
user.save(function(err) { user.save(function(err) {
done(err, user); done(err, user);
}); });