What is the best way to add custom data on user creation?

I want to include user’s phone number while creating user with Meteor’s account-password package. The related method is Accounts.createUser which accepts “username, email, password and profile” variables, optionally. I would make use of the profile object to store the custom user data but using profile object is discouraged in the guide. On the other hand, the related section in the guide suggests using Accouns.onCreateUser after Accouns.createUser for adding fields on user registration. I wonder if there is any other reliable way to create a user with custom data by using only the Accouns.createUser. What is the best practice adding custom data on user creation?

In fact, you should use onCreateUser in cases, when you want add data on backend side in most cases for fields, when you do not trust user. As example: createdAt field.
If you want add one more field you can write code in this way:
Accounts.createUser({
username,
password,
profile: {
field1, // all these fields without “:” after name is shorthands
field2: someInfo,
field3: ICollectStrangeInfo(), //
},
}, callback(error)
})

1 Like

Thank you @linegel. I didn’t know it was simple like that! By the way, I find onCreateUser very useful later on, as it allows keeping data on the server side when needed.

Keep in mind that everything you store inside the profile will be published by default. Also it’s writeable from the client by default. If that’s fine for your use case, go for it.

Otherwise use onCreateUser – that’s the way how you should set custom properties.