登录流星之前强制进行电子邮件验证

时间:2013-03-13 10:54:34

标签: login meteor

我用

Accounts.config({
    sendVerificationEmail: true, 
    forbidClientAccountCreation: false
})

在创建用户时发送电子邮件验证。但是,当用户注册时,他们可以在验证邮件之前进入产品,这是我不想要的。

我尝试通过创建模板变量来进行黑客攻击,该模板变量在验证用户时是真实的,但用户信息在渲染模板后到达,甚至使用Meteor。 setTimeout()我无法在数据到达时更新模板。

有关正确方法的任何建议吗?

的Tx

2 个答案:

答案 0 :(得分:16)

要阻止他们登录,你可以这样做:

Meteor.startup(function() {
  if (Meteor.isServer) {
    var loginAttemptVerifier = function(parameters) {
      if (parameters.user && parameters.user.emails && (parameters.user.emails.length > 0)) {
        // return true if verified email, false otherwise.
        var found = _.find(
                           parameters.user.emails, 
                           function(thisEmail) { return thisEmail.verified }
                          );

        if (!found) {
          throw new Meteor.Error(500, 'We sent you an email.');
        }
        return found && parameters.allowed;
      } else {
        console.log("user has no registered emails.");
        return false;
      }
    }
    Accounts.validateLoginAttempt(loginAttemptVerifier);
  }
});

答案 1 :(得分:9)

首先,您需要让数据“不可动摇”,看看发布功能:http://docs.meteor.com/#meteor_publish

因此,在您的产品的Meteor.publish功能中,您应该执行以下操作:

这可确保客户只有在登录后才能看到该产品。拥有经过验证的帐户。他们仍然可以登录,但在他们的帐户经过验证后无法看到产品

服务器js

Meteor.publish("productinfo", function () {
  user = Meteor.users.findOne({_id:this.userId})
  if(user) {
      if(user.emails[0].verified) {
          //You can put some extra logic in here to check which product the user has, if you're selling or something like that
          return Products.find({});
      }
   }
});

请记住,您需要删除meteor使用的autopublish以使生活更轻松,它基本上将所有集合发布给用户,但您想要限制某些信息,以便将其删除

其次,您需要处理模板上的数据,以便在用户未登录模板时,内容不可见。因此,即使在浏览器最初加载的那一步中,他们也看不到产品

客户端JS

Meteor.subscribe("productinfo");

Template.products.products = function() {
  if(Meteor.userId()) {
    if(Meteor.user().emails[0].verified) {
        return Product.findOne({_id:"your product id"});
    }
  }
}

这样模板助手会检查用户是否已登录&他们有经过验证的帐户。此外,如果在客户端更改了代码,由于发布功能,他们将无法看到该产品。