Javascript 我如何集成Google';aldeed内部的s reCaptcha:自动成型(流星)
我要做的是防止用户绕过验证码。现在,在联系人表单上,用户可以填写除验证码以外的所有字段,并且表单仍然可以提交 这是显示联系人表单的网页-> 这是页面-> 这是显示此联系人表单的代码->Javascript 我如何集成Google';aldeed内部的s reCaptcha:自动成型(流星),javascript,meteor,Javascript,Meteor,我要做的是防止用户绕过验证码。现在,在联系人表单上,用户可以填写除验证码以外的所有字段,并且表单仍然可以提交 这是显示联系人表单的网页-> 这是页面-> 这是显示此联系人表单的代码-> {{# autoForm schema='ContactSchema' id="contactForm" type="method" meteormethod="sendContact"}} {{> afQuickField name="categoryId"}}
{{# autoForm schema='ContactSchema' id="contactForm" type="method" meteormethod="sendContact"}}
{{> afQuickField name="categoryId"}}
{{> afQuickField name="email" }}
{{> afQuickField name="title" }}
{{> afQuickField name="message" rows=8 }}
<!-- googles reCaptcha , i'm using ayue:recaptcha package to render this captcha -->
{{> reCAPTCHA}}
{{/ autoForm }}
这是服务器端代码,它从联系人表单中获取sendContact方法,以及recaptcha meteor.call方法
Meteor.methods({
formSubmissionMethod: function(formData, recaptchaResponse) {
var verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(recaptchaResponse, this.connection.clientAddress);
if (!verifyCaptchaResponse.success) {
console.log('reCAPTCHA check failed!', verifyCaptchaResponse);
throw new Meteor.Error(422, 'reCAPTCHA Failed: ' + verifyCaptchaResponse.error);
} else {
console.log('reCAPTCHA verification passed!');
}
//do stuff with your formData
return true;
},
sendContact: function (doc) {
check(doc, ContactSchema);
var html = "<b>" + doc.title + "</b><br>"
+ "<b>" + doc.email + "</b><br><br>"
+ doc.message.escape();
this.unblock();
Email.send({
to: orion.dictionary.get('contact form.email') && doc.categoryId,
from: orion.config.get('MAIL_FROM'),
// subject: orion.dictionary.get('global.siteName') + ' - Contact',
subject: doc.title + ' - Contact',
replyTo: doc.email,
html: html
})
}
});
Meteor.methods({
formSubmissionMethod:函数(formData、RecaptCharResponse){
var verifyCaptchaResponse=reCAPTCHA.verifyCaptcha(recaptchaResponse,this.connection.clientAddress);
if(!verifyCaptchaResponse.success){
log('reCAPTCHA check failed!',verifyCaptchaResponse);
抛出新Meteor.Error(422,'reCAPTCHA失败:'+VerifyCaptCharResponse.Error);
}否则{
log('recomptcha验证通过!');
}
//用你的表单数据做一些事情
返回true;
},
发送联系人:功能(文档){
检查(doc、ContactSchema);
var html=“”+doc.title+“
”
+“+doc.email+”
”
+doc.message.escape();
这是unblock();
Email.send({
收件人:orion.dictionary.get('contact form.email')&&doc.categoryId,
from:orion.config.get('MAIL_from'),
//主题:orion.dictionary.get('global.siteName')+'-Contact',
主题:doc.title+“-联系人”,
回复:doc.email,
html:html
})
}
});
不要在服务器端方法上抛出错误,只需返回成功值,如:
verifyCaptchaResponse = reCAPTCHA.verifyCaptcha(this.connection.clientAddress, doc.gRecaptchaResponse);
if (verifyCaptchaResponse.data.success === false) {
return verifyCaptchaResponse.data;
}
然后在回调中返回客户端,执行以下操作:
if (result && result.success === false) {
//CAPTCHA failed
Modal.show('recaptcha');
}
return false;
不要使用“提交表单”事件,而是使用AutoForm.hooks,然后在表单的AutoForm.hooks中使用onSubmit方法
if (result && result.success === false) {
//CAPTCHA failed
Modal.show('recaptcha');
}
return false;