我希望每个在我的应用中验证其电子邮件的用户都会在之后获得某个角色。我正在使用alanning:roles包。无论如何,在他点击验证链接后,我有一个功能,为他在Mongo中设置角色。
所以我找到了这个功能,但它显然只是客户端:
// (client-side)
Template.Homepage.created = function() {
if (Accounts._verifyEmailToken) {
Accounts.verifyEmail(Accounts._verifyEmailToken, function(err) {
if (err != null) {
if (err.message = 'Verify email link expired [403]') {
console.log('Sorry this verification link has expired.')
}
} else {
console.log('Thank you! Your email address has been confirmed.')
}
});
}
};
我有在服务器端设置角色的方法
Accounts.onCreateUser(function (options, user) {
Roles.setRolesOnUserObj(user, ['employer']);
if (options.profile) {
// include the user profile
user.profile = options.profile
}
如何连接这两者,或者有更好的方法来实现这个逻辑。我知道这是现在的功能" onCreateUser"但是如果有更好的方法,就会把它分开。
答案 0 :(得分:1)
首先,安装matb33:collection-hooks包。
然后,您可以检测服务器上用户集合的更改:
var indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
Meteor.users.after.update(function(userId, doc, fieldNames, modifier, options) {
if (indexOf.call(fieldNames, "emails") >= 0 && doc.emails) {
doc.emails.forEach(function(email) {
if (email.verified === true) {
// Verified address - do something....
}
});
}
});