将 Meteor 的注册限制为来自特定域的电子邮件

Restrict registration on Meteor to emails from a particular domain

这是我找到的最接近如何限制 Meteor 注册的答案,但我不想向每个人发送邀请:How to make sign-up invitation only?

到目前为止,这就是我所拥有的,但它不起作用 - 它不接受任何注册,即使是来自我大学域的电子邮件。我做错了什么 - 我已经为此苦苦挣扎了几个小时,我就是想不通!

Accounts.validateNewUser(function (user) {

    email_regex = new RegExp("/\A[\w+\-.]+@mycollege\.edu\z/i");    
    validemail = false;
    if (email_regex.test(user.emails[0])){
    validemail = true;
    }


      if (validemail)   
        return true;
      throw new Meteor.Error(403, "You must use an mycollege.edu email to register");
    });

您可以使用 Accounts.config 创建这样的规则。它比使用 Accounts.validateNewUser:

更方便

客户端和服务器

Accounts.config({
    restrictCreationByEmailDomain: 'college.edu'
});

来自关于 restrictCreationByEmailDomain 的文档:

If set to a string, only allows new users if the domain part of their email address matches the string. If set to a function, only allows new users if the function returns true. The function is passed the full email address of the proposed new user. Works with password-based sign-in and external services that expose email addresses (Google, Facebook, GitHub). All existing users still can log in after enabling this option