Skip to content

Commit

Permalink
Add Twitter merging strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
sahat committed Feb 1, 2014
1 parent 5c9040c commit 7fcb92e
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions config/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,18 +156,50 @@ passport.use(new GitHubStrategy(secrets.github, function(req, accessToken, refre
}
}));

/**
* Sign in with Twitter.
*
* Possible authentication states:
*
* 1. User is logged in.
* a. Already signed in with Twitter before. (MERGE ACCOUNTS, EXISTING ACCOUNT HAS PRECEDENCE)
* b. First time signing in with Twitter. (ADD TWITTER ID TO EXISTING USER)
* 2. User is not logged in.
* a. Already signed with Twitter before. (LOGIN)
* b. First time signing in with Twitter. (CREATE ACCOUNT)
*/

passport.use(new TwitterStrategy(secrets.twitter, function(req, accessToken, tokenSecret, profile, done) {
if (req.user) {
User.findById(req.user.id, function(err, user) {
user.twitter = profile.id;
user.tokens.push({ kind: 'twitter', accessToken: accessToken, tokenSecret: tokenSecret });
user.profile.name = user.profile.name || profile.displayName;
user.profile.location = user.profile.location || profile._json.location;
user.profile.picture = user.profile.picture || profile._json.profile_image_url;
user.save(function(err) {
done(err, user);
});
User.findOne({ $or: [{ twitter: profile.id }, { email: profile.email }] }, function(err, existingUser) {
if (existingUser) {
existingUser.facebook = existingUser.facebook || req.user.facebook;
existingUser.github = existingUser.github || req.user.github;
existingUser.google = existingUser.google || req.user.google;
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);
existingUser.save(function(err) {
User.remove({ _id: req.user.id }, function(err) {
req.flash('info', { msg: 'Your accounts have been merged' });
return done(err, existingUser);
});
});
} else {
User.findById(req.user.id, function(err, user) {
user.twitter = profile.id;
user.tokens.push({ kind: 'twitter', accessToken: accessToken, tokenSecret: tokenSecret });
user.profile.name = user.profile.name || profile.displayName;
user.profile.location = user.profile.location || profile._json.location;
user.profile.picture = user.profile.picture || profile._json.profile_image_url;
user.save(function(err) {
done(err, user);
});
});
}
});

} else {
User.findOne({ twitter: profile.id }, function(err, existingUser) {
if (existingUser) return done(null, existingUser);
Expand Down Expand Up @@ -200,7 +232,6 @@ passport.use(new TwitterStrategy(secrets.twitter, function(req, accessToken, tok

passport.use(new GoogleStrategy(secrets.google, function(req, accessToken, refreshToken, profile, done) {
if (req.user) {

User.findOne({ $or: [{ google: profile.id }, { email: profile.email }] }, function(err, existingUser) {
if (existingUser) {
existingUser.facebook = existingUser.facebook || req.user.facebook;
Expand Down

0 comments on commit 7fcb92e

Please sign in to comment.