Managing a "rollback" in enrollment on a promise error

I am trying to manage signing up a user and initializing them during an enrollment process. In my “reset password” page, I have the following code, as a string of promises.

// client/templates/enrollment.es6
Template['Auth.enrollment'].events({
  'submit #enroll-account-form': function (e, template) {
    e.preventDefault();
    let d = template.data;

    if (AutoForm.validateForm('enrollment-form')) {
      let password = AutoForm.getFieldValue('password');

      Accounts.resetPassword(Session.get('_resetPasswordToken'), password)
        .then(function () {
          return Meteor.promise('Profiles.init', d.profile, d.groupId)
        })
        .then(function (profile, group) {
          Router.go('Users.profile', { group: group, profile: profile });
        })
        .catch(function (err) {
          // reset user here?
        });
    }
  }
});

Here’s the problem: let’s say that one of those calls fails. The user has now had their token reset so the form is, essentially, broken and they can’t resubmit.

How can I reset them to the previous state?

1 Like