Search code examples
meteormeteor-accounts

In meteor how to prevent social logins from creating an account if one is not available?


Currently, by default if I try to use social login in meteor, it will create a new account for the user if one is not available. But I don't want that. Here's what I need :

  • When the user signup, I need to provide social signup options. When the user signup with the social account, it should come back to the app where I will present the user with a form to enter extra details. I don't want to create an account until those details are filled. I will pull the name and email from social accounts.

  • At login, if the user have already connected a social account, he will be allowed to login. Otherwise he have to signup first.

How can I implement this behavior in Meteor?


Solution

  • The way I handle this is in Account.validateNewUser

    this function validates the user and returns true or false. but you can add logic to the process.

    In my case, I check if the user exists by email:

    social logins (except for Twitter) all create a user with email.

    the function contains a user object parameter with the user account info If you do a check using Accounts.findUserByEmail(<email>) you can find if the user has been created previously.

    In that case, there are 2 cases:

    • user tried to create an account with password, just return true and the rest of the user create process will prompt the user that a user already exists with this email.

    • if it's a social login, I merge the 2 user objects to make it one, keeping the original _id. then return 'true' to pass the validation process.