Merge branch 'master' of https://github.com/robjohnson189/hackathon-starter into robjohnson189-master

* 'master' of https://github.com/robjohnson189/hackathon-starter:
  Check if there is already an account that isn't linked to a google account but uses the google accounts email address
This commit is contained in:
Sahat Yalkabov
2014-02-14 11:54:47 -05:00

View File

@ -195,15 +195,22 @@ passport.use(new GoogleStrategy(secrets.google, function(req, accessToken, refre
} else { } else {
User.findOne({ google: profile.id }, function(err, existingUser) { User.findOne({ google: profile.id }, function(err, existingUser) {
if (existingUser) return done(null, existingUser); if (existingUser) return done(null, existingUser);
var user = new User(); User.findOne({ email: profile._json.email }, function(err2, existingEmailUser) {
user.email = profile._json.email; if(existingEmailUser) {
user.google = profile.id; req.flash('errors', { msg: 'There is already an account using this google account\'s email address, login and link your account to your google account from account settings' });
user.tokens.push({ kind: 'google', accessToken: accessToken }); done(err2);
user.profile.name = profile.displayName; } else {
user.profile.gender = profile._json.gender; var user = new User();
user.profile.picture = profile._json.picture; user.email = profile._json.email;
user.save(function(err) { user.google = profile.id;
done(err, user); user.tokens.push({ kind: 'google', accessToken: accessToken });
user.profile.name = profile.displayName;
user.profile.gender = profile._json.gender;
user.profile.picture = profile._json.picture;
user.save(function(err) {
done(err, user);
});
}
}); });
}); });
} }