Accounts.ValidateNewUser need help

I’m new at this… please help.
What is wrong with this code?

Accounts.validateNewUser((user) => {
  if(user.username && user.username.length < 4 && user.username.length > 16) {
    throw new Meteor.Error(403, 'Username must have 4 to 16 characters.')
  }

  if(user.username === 'root' && user.username === 'admin') {
    throw new Meteor.Error(403, 'Username cannot be root or admin.')
  }

  return true
})

My limited experience in this says, if username is less than 4 or greater than 16, throw and error (exit). if username is root or admin, throw an error (exit). if none of the above, return true. How come now, when I try a username that’s supposed to pass still fails? Also, how come my error message is still general one provided by Meteor?

whats teh error message?

Error message is: User validation fail
I tried removing return true
I tried replacing if(user.username && user.username.length < 4 …) without the user.username && so just if(user.username.length < 4 && …)
Still same error message.

try following this example and see if you still get the user validation fail message

1 Like

Thanks I got it working now after reading the doc.

Accounts.validateNewUser((user) => {
  if(user.username.length >= 4 &&
     user.username.length <= 16 &&
     user.username !== 'root' &&
     user.username !== 'admin') {
    return true
  } else {
    throw new Meteor.Error(403, 'Invalid username. Sorry!')
  }
})
1 Like