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

Sun*_*sen 11 login meteor

我用

Accounts.config({
    sendVerificationEmail: true, 
    forbidClientAccountCreation: false
})
Run Code Online (Sandbox Code Playgroud)

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

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

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

TX

cob*_*boy 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);
  }
});
Run Code Online (Sandbox Code Playgroud)

  • 虽然这是一个老问题,但我刚刚成功实施了@cobberboy的解决方案. (2认同)

Aks*_*hat 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({});
      }
   }
});
Run Code Online (Sandbox Code Playgroud)

请记住,你需要删除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"});
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

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