Accounts-password merge with accounts-google 2017 way

So this seems to be a pretty common issue in Meteor. I have configured an auth with accounts-password and with accounts-google. Each creates separate users but I need to link them. Searching for this results in outdated packages which do not support accounts-passwords anyway.
So I wonder what’s the 2017 way of merging google accounts with password accounts?

1 Like

I had the same problem recently. Its not very elegant but I managed to resolve it using the Accounts.onCreateUser() method. This is a simplified version but it checks if a user exists when using loginWithGoogle.

Accounts.onCreateUser(function (options, user) {
//Get Google email
const email = user.services.google.email;
//Check if the user exists (I've stored the email inside a profile object)
const existingUser = Meteor.users.findOne({'profile.email': email});
//If the user exists, add Google information to the user
if (existingUser) {
  existingUser['services'] = {}
  existingUser.services['google'] = user.services.google;
  // remove existing record
  Meteor.users.remove({_id: existingUser._id});
  //re-insert user 
  return existingUser;
}
//If the user doesn't exist..
if (!existingUser) {
  //manipulate user object
  //return user
  return user
}
}
2 Likes