【问题标题】:How to filter Accounts.createUser options data?如何过滤 Accounts.createUser 选项数据?
【发布时间】:2018-02-24 00:28:29
【问题描述】:

注册我的流星项目。 我正在使用“accounts-password”模块,我想知道,如何过滤或修改“options”参数中的数据?

Accounts.createUser(options, [callback]) - 它存储用户名、电子邮件、密码。

例如,我想阻止用户创建带有特殊符号 (!#@$) 的用户名,例如:username.replace(/[^A-Z0-9]/ig, "") 我该如何配置?

我正在尝试“Accounts.onCreateUser”功能,但它只对“个人资料”(附加)信息有帮助。

【问题讨论】:

  • 尝试过滤客户端 - 无效,因为有人可以使用开发者控制台注册

标签: javascript meteor passwords


【解决方案1】:

Accounts.onCreateUser() 允许您修改整个用户文档。您可以检查 username 字段并根据需要替换其中的字符。

例子:

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

【讨论】:

    【解决方案2】:

    我们来看看Accounts.onCreateUser()

    论据

    功能 功能

    每当创建新用户时调用。返回新的用户对象,或抛出Error 以中止创建。

    所以,为了防止用户在用户名中使用任何特殊字符,你应该这样做:

    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');
      }
      ...
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-20
      • 2017-11-14
      • 1970-01-01
      • 2014-01-15
      • 1970-01-01
      • 1970-01-01
      • 2011-10-01
      相关资源
      最近更新 更多