【问题标题】:Meteor Accounts.verifyEmail to set role on the userMeteor Accounts.verifyEmail 为用户设置角色
【发布时间】:2015-07-16 04:36:21
【问题描述】:

我希望每个在我的应用中验证其电子邮件的用户之后都能获得特定的角色。我正在使用 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”中,但如果有更好的方法,我会单独放置。

【问题讨论】:

    标签: meteor


    【解决方案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....
          }
        });
      }
    });
    

    【讨论】:

    • 感谢您的解决方案!无论如何会有一个使用 Accounts.verifyEmail 的标准调用的解决方案,因为我实际上只需要使用它一次,如果有变化就不要一直听:)
    猜你喜欢
    • 2017-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 2019-04-24
    • 1970-01-01
    相关资源
    最近更新 更多