我想向群组提交用户创建请求,但不确定如何在express-stormpath中执行此操作。
现在我使用注册前和注册后处理程序获取一个字段,从formData中删除它,传递给res,然后在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();
}
这似乎不是一个好主意。如果连接不可用且组分配失败会发生什么?用户可能在没有组的情况下陷入困境。
答案 0 :(得分:8)
另一种方法是将值保存到帐户的自定义数据对象中,然后使用它来创建该组。
如果您在form configuration中定义表单字段,express-stormpath会自动将表单字段保存到自定义数据:
web: {
register: {
form: {
fields: {
describes: {
enabled: true,
label: 'Position',
name: 'describes',
placeholder: 'e.g. employer, seeker',
required: true,
type: 'text'
}
}
}
}
}
然后,在您的注册后处理程序中,检索自定义数据值并使用它来分配组:
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();
}
这样,您可以保证保存该值。如果后注册处理程序由于某种原因失败,您可以通过检查帐户的自定义数据资源来查看用户应该在哪个组。