Search code examples
meteoriron-routermeteor-accountsmeteor-autoform

Meteor account enrollment


I'm looking for a way to customize logins to allow for an administrator to provide a user with an account and a one-time-use temporary password (via email), that enables the user to log into a site (the user can't create an account on their own).

The first time the user logs in, they are instructed to change the temporary password given by the administrator.

I have accounts-ui and accounts-password working, but the user can create a new account at will.

If it matters, I plan to use Autoform and Iron Router for this.

I search the Meteor docs for "enroll", but the information is sparse IMO. Is there a fully working example somewhere to help me get started?


Solution

  • To disable the usual way of creating an account, use Accounts.config:

    forbidClientAccountCreation Boolean

    Calls to createUser from the client will be rejected. In addition, if you are using accounts-ui, the "Create account" link will not be available.

    Then, instead of having a temporary password, I think you should create the account without password and then use Accounts.sendEnrollmentEmail to send the user an email to choose one.

    To create an account without a password on the server and still let the user pick their own password, call createUser with the email option and then call Accounts.sendEnrollmentEmail. This will send the user an email with a link to set their initial password.

    So, something like that:

    Accounts.config({forbidClientAccountCreation: true});
    
    Meteor.methods({
      adminCreateAccount: function (accountAttributes) {
        if(Meteor.user() && Meteor.user().role == "admin") {
          var accountId = Accounts.createUser({
            'username': accountAttributes.username,
            'email': accountAttributes.emailAddress
          });
          Accounts.sendEnrollmentEmail(accountId);
        }
      }
    });