Search code examples
meteormeteor-accounts

Meteor: add extra data to user on social sign up


In Meteor, I'd like to add a referrer field to new user signups. I can do this for users that register with a password, by passing in extra options to be used with Accounts.onCreateUser(function (options, user).

But this doesn't seem to work with social login. Any ideas how I can get this working?


Solution

  • I think I had a similar issue, and ended doing something like

    • create the user without the field you would like to pass as an option
    • ask the client for the value of that field
    • update the user

    So:

    //server
    Accounts.onCreateUser(function(options, user) {
      if (user.services.facebook)
        user.askReferer = true;
      retur user;
    });
    
    //client
    var userId;
    Tracker.autorun(function() {
      var user = Meteor.user();
      if (!user || user.id === userId || !user.askReferer) {return;}
      userId = user.id;
      var referer = ...;
      Users.update({_id: user.id}, {$set: {referer: referer}}, {$unset: {askReferer: 1}});
    });
    

    Would love if someone comes up with a better solution!