Create Stormpath user and assign to group in one call

302 views Asked by At

I'd like to submit the user creation request with the group but not sure how to do that in express-stormpath.

Right now I use the pre- and post-registration handlers to get a field, delete it from formData, pass to res, then set the group on the post registration handler:

preRegistrationHandler: function (formData, req, res, next) {
    res.locals.describes = formData.describes;
    delete formData.describes;

    next();
},

postRegistrationHandler: function (account, req, res, next) {
    var seekerHref = config.SEEKERS_HREF;
    var employerHref = config.EMPLOYERS_HREF;

    if (res.locals.describes === "seeker") {
        //Adding to seeker group
        account.addToGroup(seekerHref, function(err, membership) {
            console.log(membership);
        });
    } else if (res.locals.describes === "employers") {
        //Adding to the employer group
        account.addToGroup(employerHref, function(err, membership) {
            console.log(membership);
        });
    }
    delete res.locals.describes;

    next();
}

This doesn't seem like a good idea. What happens if the connection becomes unavailable and the group assignment fails? The user might be stuck in a limbo state without a group.

1

There are 1 answers

4
Nate Barbettini On BEST ANSWER

An alternate way of doing this is to save the value to the account's Custom Data object, and then use that to create the group afterwards.

express-stormpath will save a form field to Custom Data automatically if you define it in the form configuration:

web: {
  register: {
    form: {
      fields: {
        describes: {
          enabled: true,
          label: 'Position',
          name: 'describes',
          placeholder: 'e.g. employer, seeker',
          required: true,
          type: 'text'
        }
      }
    }
  }
}

Then, in your post-registration handler, retrieve the Custom Data value and use it to assign the group:

postRegistrationHandler: function (account, req, res, next) {
    var seekerHref = config.SEEKERS_HREF;
    var employerHref = config.EMPLOYERS_HREF;

    account.getCustomData(function(err, customData) {
        if (customData.describes === "seeker") {
            //Adding to seeker group
            account.addToGroup(seekerHref, function(err, membership) {
                console.log(membership);
            });
        } else if (customData.describes === "employers") {
            //Adding to the employer group
            account.addToGroup(employerHref, function(err, membership) {
                console.log(membership);
            });
        }

        customData.remove('describes');
        customData.save(function(err) {
            if (err) throw err;
        });
    });

    next();
}

This way, you are guaranteed that the value will be saved. If the post-registration handler fails for some reason, you can see what group the user should be in by examining the account's custom data resource.