Skip to content

Commit

Permalink
Add Merge strategy for Facebook so far
Browse files Browse the repository at this point in the history
  • Loading branch information
sahat committed Jan 31, 2014
1 parent 4e03185 commit 535fd2d
Showing 1 changed file with 41 additions and 9 deletions.
50 changes: 41 additions & 9 deletions config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,52 @@ passport.use(new LocalStrategy({ usernameField: 'email' }, function(email, passw
});
}));

/**
* Sign in with Facebook.
*
* Possible authentication states:
*
* 1. User is logged in.
* 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)
* 2. User is not logged in.
* a. Already signed with Facebook before. (LOGIN)
* b. First time signing in with Facebook. (CREATE ACCOUNT)
*/

passport.use(new FacebookStrategy(secrets.facebook, function (req, accessToken, refreshToken, profile, done) {
if (req.user) {
User.findById(req.user.id, function(err, user) {
user.facebook = profile.id;
user.tokens.push({ kind: 'facebook', accessToken: accessToken });
user.profile.name = user.profile.name || profile.displayName;
user.profile.gender = user.profile.gender || profile._json.gender;
user.profile.picture = user.profile.picture || profile._json.profile_image_url;
user.save(function(err) {
done(err, user);
});
User.findOne({ facebook: profile.id }, function(err, existingUser) {
if (existingUser) {
existingUser.github = existingUser.github || req.user.github;
existingUser.google = existingUser.google || req.user.google;
existingUser.twitter = existingUser.twitter || req.user.twitter;
existingUser.email = existingUser.email || req.user.email;
existingUser.password = existingUser.password || req.user.password;
existingUser.profile = existingUser.profile || req.user.profile;
existingUser.tokens = _.union(existingUser.tokens, req.user.tokens);

This comment has been minimized.

Copy link
@sahat

sahat Jan 31, 2014

Author Owner

Combine access tokens from both accounts.

existingUser.save(function(err) {
User.remove({ _id: req.user.id }, function(err) {
req.flash('info', { msg: 'Your accont has been merged with an existing one.' });
return done(null, existingUser);
});
});
} else {
User.findById(req.user.id, function(err, user) {
user.facebook = profile.id;
user.tokens.push({ kind: 'facebook', accessToken: accessToken });
user.profile.name = user.profile.name || profile.displayName;
user.profile.gender = user.profile.gender || profile._json.gender;
user.profile.picture = user.profile.picture || profile._json.profile_image_url;
user.save(function(err) {
done(err, user);
});
});
}
});
} else {
User.findOne({ facebook: profile.id }, function(err, existingUser) {
console.log(profile);
if (existingUser) return done(null, existingUser);
var user = new User();
user.email = profile._json.email;
Expand Down

0 comments on commit 535fd2d

Please sign in to comment.