How to update user account password, email and profile together?

how to update user account password, email and profile together?

Hello @theara Can you get the solution of How to update user account password, email and profile together?
Please share with us.

I create the method

Meteor.methods({
    userInsert: function (doc) {
        // Add account
            id = Accounts.createUser({
                username: doc.username,
                password: doc.password,
                profile: doc.profile
            });

        return id;
    },
    userUpdate: function (id, doc) {
        // Update account
            Meteor.users.update(id, {
                $set: {
                    username: doc.username,
                    profile: doc.profile
                }
            });

        // Update password
        if (doc.password != 'the same') {
            Accounts.setPassword(id, doc.password);
        }

        return true;
    },
    userRemove: function (id) {

        Meteor.users.remove(id);

        return id;
    }
});

What if the update fails? As you are returning true there is no way of knowing, right?

@chrise86 yes, we can throw a Meteor.error exception to perform what you expect
this how i am doing that:

usersProfileEdit: function(data) {
    check(data, Object);

    var error=[];
    if (Meteor.users.findOne({'emails.address': data.email, id: {$not: Meteor.userId}})) {
      error.push({name: "email", type: "exist"});
    }

    if (Meteor.users.findOne({'username': data.username, id: {$not: Meteor.userId}})) {
      error.push({name: "username", type: "exist"});
    }

    if (!Accounts._checkPassword(Meteor.user(), {digest: data.password, algorithm: 'sha-256'})) {
      error.push({name: "password", type: "wrong password"});
    }

    if (error.length) throw new Meteor.Error(422, error);
    ....
  }

then on the front (to display computed server errors with autoform )

AutoForm.addHooks('profileSubmit', {
  onSubmit: function (insertDoc, doc, currentDoc) {
    var self = this;
    Meteor.call('usersProfileEdit', doc, function(error, result) {
      if (error) {
        $('#accountSignup').find("button[type=submit]").removeAttr('disabled');
        self.validationContext.addInvalidKeys(error.reason);
      } else {
       ....
      }
    }); 
    return false;
  }
});

Hope this help someone