ValidateNewUser calling validateLoginAttempt even when throwing error

Dear Community,

I am trying to put in place a verification process for new users on account creation. As such I am using the Accounts.validateNewUser function. I manage to put it in place and make it throw errors (it even prevents creating accounts) BUT even when I throw an error it then calls Accounts.validateLoginAttempt which also throws a different error that is visible. Problem is I’d like to see the first error. Let me show you with code:

I exaggerated the code to show you the error :

Accounts.validateNewUser((user) => {
  throw new Meteor.Error("NO REGISTRATION ALLOWED");
}

Accounts.validateLoginAttempt(function(options){
  if (!options.user){
    throw new Meteor.Error("Invalid Email or Password");
  }
})

In this case I think I should get the first error (no registration allowed) as soon as someone would try to sign up but NO, I get the second one. Even though the account is not created. So validateNewUser is making the job of blocking the user creation but still continuing the Login process. Therefor I never manage to get the first error on client side :

		Accounts.createUser({email, password, username}, (err) => {
			if (err) {
				console.log(err);
				this.setState({
					error: err.reason || err.error
				})
			} else {
				Meteor.call('sendVerificationLink', (error, response) => {
					if(err){
						this.setState({
							error: err.reason || err.error
						})
					} else {
						this.setState({
							error: ""
						})	
					}
				});
			}
		});

Any idea why or how to solve this ?

No one on this one ?

The second validation is invoked but under options.allowed you are able to check if the previous one passed. Also the error which you have thrown should be accessible under options.error.

To give you a live example I would change the validateLoginAttempt to:

Accounts.validateLoginAttempt(function(options) {
  if (!options.allowed && options.error) {
    throw options.error;
  } else if (!options.user) {
    throw new Meteor.Error("Invalid Email or Password");
  }
})
1 Like