validateNewUser hook doesn't run

Hi,

I’m just starting out with Meteor.js. I have a small app running locally. I’m creating a user wth

Accounts.createUser({email:Data.email,password:Data.password}, (err) => {
if(err){
this.setState({
error: err.reason
});
}else {
this.props.history.push(’/’);
}
});.

In main.js on the server side, I’ve added two hooks,
Accounts.onCreateUser((options, user) => {
console.log(“hello”);
console.log(JSON.stringify(options));

AND

Accounts.validateNewUser((user) => {
console.log("validating "+user);
users.schema.validate(user);
if (user.username && user.username.length >= 3 && user.password.length >= 7) {
return true;
} else {
throw new Meteor.Error(403, ‘Username must have at least 3 characters and password 8 chars’);
}
});

validateNewUser doesn’t run, where onCreateUser runs. Any ideas? Is there any benefit to using validateNewUser?

According to the api both seem similar, and by throwing an error you can stop the account from being created.

Those hooks are defined on the server, and validatNewUser is called before onCreateUser. Your check about the password length etc. needs to go into validateNewUser. In the onCreateUser hook you can inject additional values server-side, like default values for things not asked in the sign-up form, creation date, etc.

I called the hook on a main.js that was on the server. Should I be using a different file?

The main.js file is fine, but I misspoke about the call order of the hooks. Accounts.onCreateUser is being called before Accounts.validateNewUser.

Accounts.onCreateUser(function(options, user){
  const customize = Object.assign({
    createdAt: new Date(),
    profile: options.profile
  }, user);

  return customize;
})
Accounts.validateNewUser(function(user){
 if(user.username.length<8) {
  throw new Meteor.Error(403, 'username must be longer than 8');
 } else {
  return true;
 }
});