Express.js Stormpath PostRegistrationHandler
Express.js Stormpath PostRegistrationHandler
风暴之路documentation
没有提到在 PostRegistrationHandler 中修改用户属性,我需要能够做到这一点。
创建用户后,我想给它一个随机字符串作为属性。这个随机字符串将成为我单独的 Mongo 数据库的关键。在我的 app.js 中,我有:
app.use(stormpath.init(app, {
postRegistrationHandler: function(account, res, next) {
// theoretically, this will give a user object a new property, 'mongo_id'
// which will be used to retrieve user info out of MONGOOOO
account.customData["mongo_id"] = "54aabc1c79f3e058eedcd2a7"; // <- this is the thing I'm trying to add
console.log("RESPNSE:\n"+res);
account.save(); // I know I'm using 'account', instead of user, but the documentation uses account. I don't know how to do this any other way
next();
console.log('User:\n', account, '\njust registered!');
},
apiKeyId: '~/.stormpath.apiKey.properties',
//apiKeySecret: 'xxx',
application: ~removed~,
secretKey: ~removed~,
redirectUrl: '/dashboard',
enableAutoLogin: true
}));
我不知道我的 console.log 行如何使用 mongo_id 属性打印出 customData。当我稍后尝试使用 req.user.customData['mongo_id'] 访问它时,它不存在。帐户和 req.user 必须不同。我怎样才能拯救用户?
我是上面提到的库的作者,所以我认为这会有所帮助。
我已经修改了您的代码以使其正常工作 =)
app.use(stormpath.init(app, {
postRegistrationHandler: function(account, res, next) {
// The postRegistrationHandler is a special function that returns the account
// object AS-IS. This means that you need to first make the account.customData stuff
// available using account.getCustomData as described here:
// http://docs.stormpath.com/nodejs/api/account#getCustomData
account.getCustomData(function(err, data) {
if (err) {
return next(err);
} else {
data.mongo_id = '54aabc1c79f3e058eedcd2a7';
data.save();
next();
}
});
},
apiKeyId: 'xxx',
apiKeySecret: 'xxx',
application: ~removed~,
secretKey: ~removed~,
redirectUrl: '/dashboard',
enableAutoLogin: true,
expandCustomData: true, // this option makes req.user.customData available by default
// everywhere EXCEPT the postRegistrationHandler
}));
希望对您有所帮助!
rdegges提供的解决方案并不完全正确。
对next()
的调用必须在customData保存完成后调用,不能立即调用,所以必须是data.save()
中的回调。
此外,显然 postRegistrationHandler
参数已更改为 account, req, res, next
。
这是一个当前有效的解决方案:
postRegistrationHandler: function(account, req, res, next) {
account.getCustomData(function(err, data) {
if (err)
return next(err);
data.mongo_id = '54aabc1c79f3e058eedcd2a7';
data.save(next);
});
},
风暴之路documentation 没有提到在 PostRegistrationHandler 中修改用户属性,我需要能够做到这一点。
创建用户后,我想给它一个随机字符串作为属性。这个随机字符串将成为我单独的 Mongo 数据库的关键。在我的 app.js 中,我有:
app.use(stormpath.init(app, {
postRegistrationHandler: function(account, res, next) {
// theoretically, this will give a user object a new property, 'mongo_id'
// which will be used to retrieve user info out of MONGOOOO
account.customData["mongo_id"] = "54aabc1c79f3e058eedcd2a7"; // <- this is the thing I'm trying to add
console.log("RESPNSE:\n"+res);
account.save(); // I know I'm using 'account', instead of user, but the documentation uses account. I don't know how to do this any other way
next();
console.log('User:\n', account, '\njust registered!');
},
apiKeyId: '~/.stormpath.apiKey.properties',
//apiKeySecret: 'xxx',
application: ~removed~,
secretKey: ~removed~,
redirectUrl: '/dashboard',
enableAutoLogin: true
}));
我不知道我的 console.log 行如何使用 mongo_id 属性打印出 customData。当我稍后尝试使用 req.user.customData['mongo_id'] 访问它时,它不存在。帐户和 req.user 必须不同。我怎样才能拯救用户?
我是上面提到的库的作者,所以我认为这会有所帮助。
我已经修改了您的代码以使其正常工作 =)
app.use(stormpath.init(app, {
postRegistrationHandler: function(account, res, next) {
// The postRegistrationHandler is a special function that returns the account
// object AS-IS. This means that you need to first make the account.customData stuff
// available using account.getCustomData as described here:
// http://docs.stormpath.com/nodejs/api/account#getCustomData
account.getCustomData(function(err, data) {
if (err) {
return next(err);
} else {
data.mongo_id = '54aabc1c79f3e058eedcd2a7';
data.save();
next();
}
});
},
apiKeyId: 'xxx',
apiKeySecret: 'xxx',
application: ~removed~,
secretKey: ~removed~,
redirectUrl: '/dashboard',
enableAutoLogin: true,
expandCustomData: true, // this option makes req.user.customData available by default
// everywhere EXCEPT the postRegistrationHandler
}));
希望对您有所帮助!
rdegges提供的解决方案并不完全正确。
对next()
的调用必须在customData保存完成后调用,不能立即调用,所以必须是data.save()
中的回调。
此外,显然 postRegistrationHandler
参数已更改为 account, req, res, next
。
这是一个当前有效的解决方案:
postRegistrationHandler: function(account, req, res, next) {
account.getCustomData(function(err, data) {
if (err)
return next(err);
data.mongo_id = '54aabc1c79f3e058eedcd2a7';
data.save(next);
});
},