Custom form to create users

Hello,

I want to disable users for signing for themselves instead I want to create a form where logged in users can create new users, eg. passwords, profile name, usernames, emails etc. Also preferably I’d like to have an isAdmin bool, so that only isAdmin can see and add other users.

Is there an article about this available? Did anyone do this before?
PS.
I haven’t done this before, got no idea how it is achievable, there’s no such thing in the

Yeah you could do this for sure.

Look at the accounts API. Here’s a snippet from my code that may get you started.

Accounts.createUser({
			email: $('[name=email]').val(),
			password: $('[name=password]').val(),
			profile: {
				name_first:  $('[name=name_first]').val(),
				name_last:  $('[name=name_last]').val(),
				username:  ($('[name=name_first]').val()+$('[name=name_last]').val()+random_id).toLowerCase(),
			},
			username:  $('[name=name_first]').val()+$('[name=name_last]').val()+random_id, // Must be doubled here, sadly, for the Guest Account logic. Cleanup one day...
		}

So as you see, you could run this from anywhere to create a new user

Note: Just had a user sign up with a space in their name, make sure to strip the usernames here with regex for invalid characters!

1 Like

Do not forget to put forbidClientAccountCreation: false, to give client-side permissions for user creation.

if (Meteor.isServer) {
Accounts.config({
// enable client user creation
forbidClientAccountCreation: false,
}),

Does this go to client main.js? And how do I use it?

You can use it anywhere, I use it in my /client/templates/register/register.js file for example. I use IronRouter to do stuff.

Nice, it does work. Very smoothly too.
I use it like

"submit .add-account": function(event, template){

event.preventDefault();

var useremail = event.target.user_email.value;
var userpwd = event.target.user_pwd.value;
var userfirst = event.target.user_first.value;
var userlast = event.target.user_last.value;
var userusername = event.target.user_username.value;

console.log(useremail, userpwd, userfirst, userlast, userusername);


Accounts.createUser({
			email: useremail,
			password: userpwd,
			profile: {
				name_first:  userfirst,
				name_last:  userlast,
				username:  userusername,
			},
			username:  userusername, 
		});


}

});

However, for some reason it logs me in onto that account. How do I stop that?

Hey perfect, glad it was of use to you.

Hrm not really sure how to avoid that.

Why don’t you just do a Meteor.logout(); and the Router.go("/somewhere"); until you get the right solution working. Bit hacky…

Read thru this: https://docs.meteor.com/api/accounts.html

Added it to main.js (server)
as

Meteor.startup(() => {
Accounts.config({
// enable client user creation
forbidClientAccountCreation: false,
})


Ah very cool! Thanks for sharing. It will come in handy for API building :slight_smile: