在登录meteor之前强制进行电子邮件验证 [英] Force email validation before login meteor
问题描述
我用
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.
我尝试通过创建一个模板变量来破解它,该变量在用户验证时为真,但用户信息在模板渲染后到达,甚至使用流星.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?
发送
推荐答案
首先,你需要让你的数据不可破解",看看发布函数:http://docs.meteor.com/#meteor_publish
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.
这篇关于在登录meteor之前强制进行电子邮件验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!