Steeve Cannon
Steeve Cannon

Reputation: 3682

How can I create users server side in Meteor?

In the new Meteor auth branch how can I create users server side?

I see how to create them client side with the call to

[Client] Meteor.createUser(options, extra, callback)

But suppose I want to create a Meteor user collection record on startup?

For example, the Administrator account during startup/bootstrapping for an application?

Thanks Steeve

Upvotes: 15

Views: 25379

Answers (7)

Vishal Wayachal
Vishal Wayachal

Reputation: 49

Create user from server side

    // Server method
    Meteor.methods({
      register: function(data) {
       try {
         console.log("Register User");
         console.log(data);
         user = Accounts.createUser({
           username: data.email,
           email: data.email,
           password: data.password,
           profile: {
             name: data.email,
             createdOn: new Date(),
             IsInternal: 0
           }
         });
         return {
           "userId": user
         };
       } catch (e) {
         // IF ALREADY EXSIST THROW EXPECTION 403
         throw e;
       }
     }
    });
    // Client call method
    Meteor.call('register',{email: "[email protected]",password: "123456"}, function(error, result){
      if(result){
       console.log(result)
      }
      if(error){
       console.log(result)
      }
    });

Upvotes: 0

Maciek Leks
Maciek Leks

Reputation: 1448

Working coffeescript example for Meteor version 1.1.0.2 (server side):

userId = Accounts.createUser  
    username: 'user'
    email: '[email protected]'
    password: 'password'
    profile:
      name: 'user name' 
user = Meteor.users.findOne userId 

I struggled for some time with this API getting 'User already exists' exception in working code before adding profiles.name to the options and exception disappeared.

reference: Accounts.createUser(options,[callback])

Upvotes: 0

andy
andy

Reputation: 320

On newer versions of meteor use

  Accounts.createUser({
                            username: username,
                            email : email,
                            password : password,
                            profile  : {
                                //publicly visible fields like firstname goes here
                            }

    });

note: the password hash is generated automatically

On older versions of meteor use:

1 - NB: DO YOU HAVE THE REQUIRED PACKAGES INSTALLED ?

  • mrt add accounts-base
  • mrt add accounts-password

On some versions of meteor you cannot call SRP password salt generator as Steeve suggested, so try this:

2 - do Meteor.users.insert( )

e.g.

 var newUserId = 
 Meteor.users.insert({
         emails: ['[email protected]'],
         profile  : { fullname : 'peter' }
 });

note: a user must have EITHER a username or an email address. I used email in this example.

3 - Finally set the password for the newly created account.

      Accounts.setPassword(newUserId, 'newPassword');
      

Upvotes: 22

Lloyd Dewolf
Lloyd Dewolf

Reputation: 396

I've confirmed that the following code in my server/seeds.js file works with the most recent version of Meteor (Release 0.8.1.1)

if (Meteor.users.find().count() === 0) {
  seedUserId = Accounts.createUser({
    email: '[email protected]',
    password: '123456'
  });
}

Directory (or folder) of server means I'm running the code on the server. The filename seeds.js is completely arbitrary.

The official documentation now describes both the behavior for Accounts.createUser() when run on the client and when run on the server.

Upvotes: 1

Qichao Dong
Qichao Dong

Reputation: 364

Probably it's a well known fact now, but for the sake of completing this - there's a new server API for doing this on the auth branch. From the docs on auth:

" [Server] Meteor.createUser(options, extra) - Creates a user and sends that user an email with a link to choose their initial password and complete their account enrollment

options a hash containing: email (mandatory), username (optional) extra: extra fields for the user object (eg name, etc). "

Please note the API is subject to change as it's not on the master branch yet.

Upvotes: 8

Steeve Cannon
Steeve Cannon

Reputation: 3682

For now this has been suggested in the meteor-core google group.

Meteor.users.insert({username: 'foo', emails: ['[email protected]'], name: 'baz', services:     {password: {srp: Meteor._srp.generateVerifier('password')}}});

It works. I tested it in during startup/boot strap.

I would not consider this the permanent or long term answer because I believe the auth branch is still in a great degree of change and I imagine the team behind Meteor will provide some kind of functionality for it.

So, do not depend on this as a long term answer.

Steeve

Upvotes: 8

David Glasser
David Glasser

Reputation: 1468

At the moment, I believe you cannot. Running

Meteor.call('createUser', {username: "foo", password: "bar"});

comes close, but the implementation of createUser in passwords_server.js calls this.setUserId on success, and setUserId cannot be called on the server unless we're in a client-initiated method invocation (search for "Can't call setUserId on a server initiated method call" in livedata_server.js.

This does seem like something worth supporting. Perhaps the last three lines of createUser, which log the user in, should be controlled by a new boolean login option to the method? Then you could use

Meteor.call('createUser', {username: "foo", password: "bar", login: false});

in server bootstrap code.

Upvotes: 2

Related Questions