Don't change user login when create new user?


#1

I use ian:accounts-ui-bootstrap-3 for Account UI.
And then I create new form to create new user by myself (When login).
But i don’t want to change user login when create new (stay the old user account).


#2

If you don’t want to login as the new user, you need to use createUser on the server only. You could check for Meteor.isServer before calling createUser.


#3

Something like

var pupilId = Accounts.createUser({
  username: 'username',
  password: 'password'
});

#4

From the Accounts.createUser documentation:

On the client, this function logs in as the newly created user on successful completion. On the server, it returns the newly created user id.

I’d suggest to write your own server-side only method to be called from the client to create a new user (probably allowing its execution by admin users only…)

So, on the server:

Meteor.methods({
  createNewUser: function(options){
    if (!Roles.userIsInRole(this.userId, ['admin'])) {
      throw new Meteor.Error('not enough rights', 'Only admins can create new users!');
    }
    var newUserId = Accounts.createUser(options);
    // Possibly send out an enrollment email...
    // Accounts.sendEnrollmentEmail(newUserId);

    return newUserId;
  }
})

from the client, within some template event for your new user form:

Template.newUser.events({
  // Form submit
  "submit #at-pwd-form": function(event, t) {
    // Collect data from the form...
    // ...
    var options = {
      // Data collected from the form...
    };
    Meteor.call('createNewUser', options, function(err, result) {
      if (err)
        alert('Error creating new user! ' + err.details);
      else
        alert('new user created with id ' + result);
    });
  }
});