Validating new user document serverside

Hi! I’m having trouble with this seemingly trivial stuff, harrrr!
I have this user document:

userData = {
  account: {
    type: 'free'
  profile: {
    name: 'Artem',
  username: '',
  password: '123'

Which I’m sending client-side: Accounts.createUser(userData);
Then server side I want to check if account type equals ‘free’. If it doesn’t - I want to abort new user creation (and hopefully throw error client side)
There are 2 functions which I’ve found in the docs that presumably can help me do it:

  • Accounts.validateNewUser
    Problem: it receives ‘trimmed-down’ user object which doesn’t contain properties other than profile, username, password, email. Thus I cannot validate account.type as it doesn’t exist on user object being validated.
  • Accounts.onCreateUser
    Problem: it is called after a generic user object is created and there is no way I can cancel inserting new document in Users collection. It absolutely requires to return a user document. If I return undefined it throws errors on server:
    Exception while invoking method 'createUser' Error: insert requires an argument
    It also doesn’t allow to throw method errors (as it’s not a method) -> thus I cannot log error client side.

One option is to use a method and create the new user on the server. Thats what I did for my application, but I don’t know if its the best thing to do…
Here is a blog post from David Weldon on how to hash the password (so that the password is not send in cleartext)

1 Like