Add Twitter merging strategy
This commit is contained in:
@ -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) {
|
passport.use(new TwitterStrategy(secrets.twitter, function(req, accessToken, tokenSecret, profile, done) {
|
||||||
if (req.user) {
|
if (req.user) {
|
||||||
User.findById(req.user.id, function(err, user) {
|
User.findOne({ $or: [{ twitter: profile.id }, { email: profile.email }] }, function(err, existingUser) {
|
||||||
user.twitter = profile.id;
|
if (existingUser) {
|
||||||
user.tokens.push({ kind: 'twitter', accessToken: accessToken, tokenSecret: tokenSecret });
|
existingUser.facebook = existingUser.facebook || req.user.facebook;
|
||||||
user.profile.name = user.profile.name || profile.displayName;
|
existingUser.github = existingUser.github || req.user.github;
|
||||||
user.profile.location = user.profile.location || profile._json.location;
|
existingUser.google = existingUser.google || req.user.google;
|
||||||
user.profile.picture = user.profile.picture || profile._json.profile_image_url;
|
existingUser.email = existingUser.email || req.user.email;
|
||||||
user.save(function(err) {
|
existingUser.password = existingUser.password || req.user.password;
|
||||||
done(err, user);
|
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 {
|
} else {
|
||||||
User.findOne({ twitter: profile.id }, function(err, existingUser) {
|
User.findOne({ twitter: profile.id }, function(err, existingUser) {
|
||||||
if (existingUser) return done(null, existingUser);
|
if (existingUser) return done(null, existingUser);
|
||||||
@ -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) {
|
passport.use(new GoogleStrategy(secrets.google, function(req, accessToken, refreshToken, profile, done) {
|
||||||
if (req.user) {
|
if (req.user) {
|
||||||
|
|
||||||
User.findOne({ $or: [{ google: profile.id }, { email: profile.email }] }, function(err, existingUser) {
|
User.findOne({ $or: [{ google: profile.id }, { email: profile.email }] }, function(err, existingUser) {
|
||||||
if (existingUser) {
|
if (existingUser) {
|
||||||
existingUser.facebook = existingUser.facebook || req.user.facebook;
|
existingUser.facebook = existingUser.facebook || req.user.facebook;
|
||||||
|
Reference in New Issue
Block a user