登录流星之前强制进行电子邮件验证 [英] Force email validation before login meteor

查看:131
本文介绍了登录流星之前强制进行电子邮件验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用

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

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

to send an email verification when users are created. However, when users signup, they are allowed into the product before they validate their mail, which I don't want.

我试图通过创建一个模板变量来破解它,该变量在验证用户时为true,但是用户信息是在渲染模板后甚至使用流星后到达的. setTimeout()当数据到达时,我无法更新模板.

I tried hacking it by creating a template variable that is true when the user is verified, but the user information arrives after the template is rendered, and even with a Meteor. setTimeout() I haven't been able to update the template when the data arrives.

有什么建议可以实现此目的吗?

Any suggestions for the proper way to do this?

Tx

推荐答案

首先,您需要使数据不可破解",看看发布功能:

Firstly, you you need to make your data 'unhackable', have a look at the publish functions : http://docs.meteor.com/#meteor_publish

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

So in your Meteor.publish function for your product you should do something like:

这可确保客户端只有在登录&时才能看到产品.有一个经过验证的帐户.他们仍然可以登录,但是在验证其帐户之前看不到产品.

This makes sure the client can only see the product if they are logged in & have a verified account. They can still log in but can't see the products until their account is verified.

服务器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({});
      }
   }
});

请记住,您需要删除autopublish,它使流星变得更轻松,它基本上将所有集合发布给用户,但是您想限制某些信息,因此您应该删除它

Keep in mind you need to remove autopublish which meteor uses to make life a bit easier, it basically publishes all the collections down to the user, but you want to restrict certain info so you should remove it

第二,您需要处理模板上的数据,以便在用户未登录的情况下看不到模板内容.因此,即使在最初加载浏览器的这一步中,他们也不会看到产品

Secondly you need to handle the data on your template so that if the user is not logged in the template stuff isn't visible. So even in that step when the browser is initially loading they won't see the products

客户端JS

Meteor.subscribe("productinfo");

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

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

This way the template helper checks if the user is logged in & they have a verified account. In addition if the code was changed on the client side they would not see the product because of the publish function.

这篇关于登录流星之前强制进行电子邮件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆