0

How do I add the following code to the accounts-ui package so that a profile is created at the time the user is created?

    if (options.profile) 
    {
      user.profile = options.profile;
    }

If the accounts-ui form cannot be changed would it be possible just to add an empty object to the profile at the time the account is created?

Something like this perhaps:

user.profile = {};

I was looking for this package in the "packages" directory but didn't find it, why is it not there? I thought this code would be easily available for any necessary modification.

Any advice on this would be great.

Thanks in advance :)

user1532669
  • 2,288
  • 4
  • 36
  • 72

1 Answers1

4

There's no need to add anything to any package. There is a hook available from the Accounts package that is run whenever a user is created. Accounts.onCreateUser You can use it to fill out default values for the user document. You return the user object in the state you want it to be saved.

It should be run on the server. For example, create the file server/usersetup.js and add the following code.

Accounts.onCreateUser(function(options, user) {
    if (! options.profile) options.profile = {};
    options.profile.artist = true;
    options.profile.reputation = 100;
    options.profile.someObject = {a: [], b: {}};

    user.profile = options.profile;
    return user;
});

Here's the documentation.

Julian Mann
  • 6,256
  • 5
  • 31
  • 43
  • That's perfect thank you. I'll go and read up on helpers for a bit more info. If I created a profile that contained firstName and lastName but then later wanted to add address and postcode would that effect users that only had firstName and lastName in their profile? I'm assuming that there is a way to add these additional profile elements to users with profiles that only contain firstName and lastName elements. Thanks again! – user1532669 Aug 15 '14 at 18:37
  • You can add fields to mongoDB documents as and when you need to. The structure is not as rigid as SQL. You don't need to add empty placeholders like {firstname: ""}, and you can add address and postcode values to documents with or without name fields. The main reason IMO to have a consistent structure is to cut down on checking for undefined values while displaying things. Alternatively, you can enforce that users provide a name if they want to add an address, with form validation and in Meteor.methods. I'd say the best resource to learn about this is "Discover Meteor" – Julian Mann Aug 15 '14 at 20:28
  • Thank you very much for your reply. I'll need to study this further, thanks again :) – user1532669 Aug 16 '14 at 17:54