Accounts.onCreateUser

Hi! I’m trying to create a new user using Accounts and create IAMuser.
The IAM resources are created but I don’t have the user on Mongo. If I commented the creation of IAM resources, the user is created on Mongo. I don’t understand it. Do you have any idea?

Thanks

  async createUser(user) {
    try {
      //create IAM USER
      const userName = `${user.firstName}${user.lastName}`;
      await this.createIAMUser(userName, user.company);

      //add user to the IAMgroup
      await this.addIAMUserToGroup(userName, user.company);

      //add user to nonCompanyGroupCommon group
      await this.addIAMUserToGroup(
        userName,
        'nonCompany',
        user.company
      );

      // create user accessKey on IAM service
      const awsUserKeysInfo = await this.createIAMUserAccessKey(
        userName,
        user.company
      );

      Accounts.onCreateUser((options, user) => {
        if (!options) {
          throw Error('Could not create the user');
        }
        user.firstName = options.firstName;
        user.lastName = options.lastName;
        user.company = options.company;

        user.accessKeyId = awsUserKeysInfo.AccessKeyId;
        user.secretAccessKey = awsUserKeysInfo.SecretAccessKey;
      

        if (!user.accessKeyId) {
          throw Error('Could not create the user');
        }

        return user;
      });
    

      return user;
    } catch (error) {
      await putLogsErrorsOnCloudWatch({
        company: user.company,
        error: error,
        errorMessageLabel: 'createUserError',
        logGroup: constants.CLOUDWATCH,
      });
    }
  }

this block of code should not be here. You should put it in a “startup script”, inside a Meteor.startup() function

@minha Only the question. Why the code must be inside Meteor.startup method? Cannot be this code in separate file imported directly to server main.ts (but not encapsulated into Meteor.startup)? Because I’m using it without Meteor.startup and it works without any problems, but maybe, I’m doying it wrong :smiley:

If it works, it works :slight_smile:
Put it in Meteor.startup() to make sure all Meteor core files / environment are loaded.

1 Like

I’m asking, because I didn’t find the clean explanation, when I need to put something to Meteor.startup and when not. Therefore, in this case, I’m not using meteor startup on all of my apps and startup I’m using to mongo data insert/manipulation if needed.

I’m using the startup in that case, when I would like to run some code, after Meteor server is completely started.

It’s your code, your choice, your responsible. You don’t have to do this way or that way. All info here is just suggestion, sometime it would be incorrect.

1 Like