Skip to content

Instantly share code, notes, and snippets.

@QuincyLarson
Created December 30, 2014 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save QuincyLarson/3b8edb1f1a5cb33ea7b8 to your computer and use it in GitHub Desktop.
Save QuincyLarson/3b8edb1f1a5cb33ea7b8 to your computer and use it in GitHub Desktop.
// Sign in with Twitter.
passport.use(
new TwitterStrategy(
secrets.twitter, function(req, accessToken, tokenSecret, profile, done) {
if (req.user) {
User.findOne({ twitter: profile.id }, function(err, existingUser) {
if (err) { return done(err); }
if (existingUser) {
req.flash('errors', {
msg: [
'There is already a Twitter account that belongs to you. ',
'Sign in with that account or delete it, then link it with ',
'your current account.'
].join('')
});
done();
} else {
User.findById(req.user.id, function(err, user) {
if (err) { return done(err); }
user.twitter = profile.id;
user.tokens.push({
kind: 'twitter',
accessToken: accessToken,
tokenSecret: tokenSecret
});
user.profile.name = user.profile.name || profile.displayName;
user.profile.username = user.profile.username || profile.username;
user.profile.location =
user.profile.location || profile._json.location;
user.profile.picture =
user.profile.picture || profile._json.profile_image_url_https;
user.profile.username = profile.username;
user.save(function(err) {
req.flash('info', { msg: 'Twitter account has been linked.' });
done(null, user);
});
});
}
});
} else {
User.findOne({ twitter: profile.id }, function(err, existingUser) {
if (err) { return done(err); }
var user = existingUser || new User();
if (req.user.id == user.id) {
user.twitter = profile.id;
user.email = user.email || '';
user.tokens.push({
kind: 'twitter',
accessToken: accessToken,
tokenSecret: tokenSecret
});
user.profile.name = user.profile.name || profile.displayName;
user.profile.username = user.profile.username || profile.username;
user.profile.location =
user.profile.location || profile._json.location;
user.profile.picture =
user.profile.picture || profile._json.profile_image_url_https;
user.save(function (err) {
if (err) {
return done(err);
}
done(null, user);
});
} else {
{ return done("Login failed - please try again"); }
}
});
}
})
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment