0
votes

Making a registration part on my meteor project. I'm using an "accounts-password" module and I want to know, how can I filter or modify data in "options" parameter?

Accounts.createUser(options, [callback]) - it stores username, email, password.

For example, I want to prevent users create usernames with special symbols (!#@$), like : username.replace(/[^A-Z0-9]/ig, "") How I can configure that?

I was trying "Accounts.onCreateUser" function, but it's only helps with "profile"(additional) info.

2
Try to filter client side - is not effective, because somebody can use developer console to register - Oleg Smirnoff

2 Answers

0
votes

Accounts.onCreateUser() allows you to modify the entire user document. You can examine the username field and replace characters there as much as you'd like.

Example:

Accounts.onCreateUser((options, user) => {
  user.username = user.username.replace(/[^A-Z0-9]/ig, "")
  return user;
});
0
votes

Let's have a look at Accounts.onCreateUser():

ARGUMENTS

func Function

Called whenever a new user is created. Return the new user object, or throw an Error to abort the creation.

So, in order to prevent users from using any special characters in username you should do something like this:

Accounts.onCreateUser((options, user) => {
  const re = new RegExp('^[a-z0-9]+$', 'i');
  if (!re.test(user.username)) {
    throw new Error('invalid-username', 'Entered username is invalid');
  }
  ...
});