Login with Google with existing account (email)

I’m using ‘accounts-google’ and ‘accounts-password’.
If a user signed in with some email using ‘accounts-password’ and later logged in via Google, a new account is created in the ‘Users’ collection. How can I make it use the existing user document?

You can manually check for existing email address:

import { Accounts } from 'meteor/accounts-base';
Meteor.startup(function() {
  Accounts.onCreateUser((options, user) => {
    const { google } = user.services;
    if (google) {
      const { email } = google;
      const existingUser = Accounts.findUserByEmail(email);
      if (existingUser) {
        // link existing user to google service
        Meteor.users.update({ _id: existingUser._id }, {
          $set: {
            'services.google': google,
          },
        });
        // then abort the user creation
        throw new Meteor.Error('some_error_code', 'some message');
      }
    }
    return user;
  });
});

for more information: https://docs.meteor.com/api/accounts-multi.html#AccountsServer-onCreateUser

1 Like