Create a user without using Accounts.createuser

I have attached a simpleschema to meteor.users collection and I wanted to know if I could create an user without using Accounts.createuser and just using meteor.users.allow({ insert: function() …}.

Any type help is appreciated.

Here are my schemas:

const UserProfile = new SimpleSchema({
  Age: {
    type: Number
  },
  Job: {
    type: String
  },
  Phone: {
    type: Number
  },
  Address: {
    type: String
  },
  Gender: {
    type: String
  },
  City: {
    type: String
  },
  SecurityNumber: {
    type: Number
  },
  HealthInsurance: {
    type: Number
  },
  Rnc: {
    type: String
  },
  Exequatur: {
    type: Number
  }
});

const UserSchema = new SimpleSchema({
  FirstName: {
    type: String
  },
  SecondName: {
    type: String
  },
  Surname: {
    type: String
  },
  LastName: {
    type: String
  },
  BirthDate: {
    type: Date
  },
  Password: {
    type: String
  },
  Profile: {
    type: UserProfile
  },
  CreatedAt: {
    type: Date,
    autoValue: function () {
      return new Date()
    }
  },
  roles: {
    type: String,
    optional: true,
    blackbox: true,
    allowedValues: ['client', 'secretary', 'provider', 'admin']
  }
});

Meteor.users.attachSchema(UserSchema);

Meteor.users.allow({
  insert: function () {
    return true;
  },
  update: function () {
    return true;
  },
  remove: function () {
    return true;
  }
});

And here’s is the code for creating the user just in case, just looking for new ways of doing it.

Template.signup.events({
  "submit .signupForm": function (event) {
    var name = trimInput(event.target.name.value);
    var secondName = trimInput(event.target.secondName.value);
    var surName = trimInput(event.target.surName.value);
    var lastName = trimInput(event.target.lastName.value);
    var email = trimInput(event.target.email.value);
    var password = trimInput(event.target.password.value);
    var password2 = trimInput(event.target.password2.value);

    //If any of the fills is empty then show me an alert, if the user filled everything send me to the login section
    if (isNotEmpty(name) &&
      isNotEmpty(surName) &&
      isNotEmpty(lastName) &&
      isEmail(email) &&
      isNotEmpty(password) &&
      areValidPasswords(password, password2)) {

      Accounts.createUser({
        username: name + ' ' + secondName + ' ' + surName + ' ' + lastName,
        email: email,
        password: password,
      }, function (err) {
        if (err) {
          Bert.alert(err.reason, "danger", "growl-top-right");
        } else {
          Bert.alert("Registro exitoso!", "success", "growl-top-right");
          Router.go("/");
        }
      });

    }

    return false; //prevent submit of the form

  }

});

Is there a reason you don’t want to use Accounts.createUser? Why re-invent the wheel?

Also can you edit your post and format your code by wrapping it with triple backticks ```?

1 Like

Just looking different ways of doing it, also because I’m the type of person who prefers to do things manually, it gives me more control of what I’m doing and I know every little detail of what i did.

Also thanks I was actually looking for a way to format my code.

Then it depends if you want to manage your own accounts system from scratch, or create an external auth provider for Meteor accounts?

Because Accounts.createUser comes from accounts-password, you can choose not to use that package and substitute it with your own auth system, then use Meteor.loginWith<ExternalService>

I’d probably start by digging into the code to see what Accounts.createUser does so you can modify / replicate it:

1 Like

I actually want to do that, thanks for the help!