How to get user ID during user creation in Meteor?

I create default users on a server with the Meteor launch feature. I want to create a user and also check his / her email at startup (I assume that you can only do this after creating an account).

Here is what I have:

Meteor.startup(function() {
  // Creates default accounts if there no user accounts
  if(!Meteor.users.find().count()) {
    //  Set default account details here
    var barry = {
      username: 'barrydoyle18',
      password: '123456',
      email: 'myemail@gmail.com',
      profile: {
        firstName: 'Barry',
        lastName: 'Doyle'
      },
      roles: ['webmaster', 'admin']
    };

    //  Create default account details here
    Accounts.createUser(barry);

    Meteor.users.update(<user Id goes here>, {$set: {"emails.0.verified": true}});
  }
});

As I said, I assume that the user must be created first before setting the check flag as true (if this statement is false, please show the solution to make the flag true when creating the user).

To set the email authentication flag, I know that I can update the user after creating with Meteor.users.update(userId, {$set: {"emails.0.verified": true}});.

, , , ?

+4
1

, Accounts.createUser():

var userId = Accounts.createUser(barry);
Meteor.users.update(userId, {
    $set: { "emails.0.verified": true}
});

, Accounts.onCreateUser():

var barry = {
  username: 'barrydoyle18',
  password: '123456',
  email: 'myemail@gmail.com',
  profile: {
    firstName: 'Barry',
    lastName: 'Doyle'
  },
  isDefault: true, //Add this field to notify the onCreateUser callback that this is default
  roles: ['webmaster', 'admin']
};

Accounts.onCreateUser(function(options, user) {
    if (user.isDefault) {
        Meteor.users.update(user._id, {
            $set: { "emails.0.verified": true}
        });
    }
});
+6

Source: https://habr.com/ru/post/1625402/


All Articles