我已将reCaptcha添加到我的Firebase项目中。使用发送按钮,我正在使用grecaptcha.getResponse()
向我的服务器发送表单数据以及来自验证码的响应。
这是client.js代码:
$('.sendUrl').on('click', function(e){
e.preventDefault() ;
$('#nonauthForm').validate({
rules: {
url_nauth: {
required: true,
url: true
}
}
}) ;
if( $('#nonauthForm').valid()){
var captcha_val = grecaptcha.getResponse(widget1) ;
$.post('/nonauth_url',{
URL: $('#url').val(),
CAPTCHA: captcha_val
},function(data, status){
if(status === 'success'){
if(data.val === 1){
$('#newurl').html(`${window.location.origin+ '/'+ data.code}`) ;
$('#url').val('') ;
grecaptcha.reset(widget1) ;
}
else if( data.val === 0 ){
alert('Please select the captcha to continue !!') ;
}
else if( data.val === -1){
alert('Failed to verify captcha !!') ;
grecaptcha.reset(widget1) ;
}
}
}).fail(function(res){
errorAlert('Error occurred', 'An error happend while connecting to the server !') ;
}) ;
}
else{
$('label.error').addClass('text-danger d-block mt-2');
}
}) ;
这是我的节点服务器代码(index.js):
const express = require('express') ;
const urlId = require('shortid') ;
const string = require('randomstring') ;
const app = express() ;
require('dotenv').config() ;
const secretkey = process.env.SECRETKEY ;
app.post('/nonauth_url', (req, res) => {
let captchaRes = req.body.CAPTCHA ;
if(captchaRes === undefined || captchaRes === '' || captchaRes === null) {
return res.send({
val: 0 ,
code: null
}) ;
}
let verification_url = `https://www.google.com/recaptcha/api/siteverify?secret=${secretkey}&response=${captchaRes}&remoteip=${req.connection.remoteAddress}` ;
request(verification_url, function(err,response,body){
let data = JSON.parse(body) ;
if( data.success !== undefined && !data.success){
return res.send({
val: -1,
code: null
}) ;
}
let codeGenerated = 'n' + urlId.generate() ; // n for non-authenticated users
let docname = 'doc-' + string.generate(4) ;
let schema = {
code: codeGenerated,
url: req.body.URL,
expiredAt: Date.now()+600000 // in milisceonds for 10min
}
let docRef = db.collection('nonauth_url').doc(docname).set(schema).then(() => {
res.send({
val: 1,
code: schema.code
}) ;
}).catch(err => {
console.log(err) ;
}) ;
}) ;
}) ;
exports.routeFunc = functions.https.onRequest(app) ;
因此,如果用户不检查验证码,它将返回代码0,成功的代码1和验证失败的代码-1。这段代码可以在localhost上正常运行。但是在我的托管网址**。firebaseapp.com上,如果我没有检查验证码,它会回馈对验证码的要求。但是,当我检查Recaptcha并将请求发送到服务器时,它给出的错误代码为500。
编辑:现在,我使用axios代替了请求
新代码:
axios.get(verification_url).then( res => {
return res.data ;
}).then( object => {
if(object.success){
let codeGenerated = 'n' + urlId.generate() ; // n for non-authenticated users
let docname = 'doc-' + string.generate(4) ;
let schema = {
code: codeGenerated,
url: req.body.URL,
expiredAt: Date.now()+600000 // in milisceonds for 10min
}
let docRef = db.collection('nonauth_url').doc(docname).set(schema).then(() => {
res.send({
val: 1,
code: schema.code
}) ;
}).catch(err => {
console.log(err) ;
}) ;
}
else{
res.send({
val: -1,
code: null
}) ;
}
}).catch(err => {
console.log("Error occurred in axios",err) ;
}) ;
}) ;
现在出现以下错误:
Error occurred in axios
{ Error: getaddrinfo EAI_AGAIN www.google.com:443
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:67:26)
errno: 'EAI_AGAIN',
code: 'EAI_AGAIN',
syscall: 'getaddrinfo',
hostname: 'www.google.com',
host: 'www.google.com',
port: 443
.
.
.
.
}
答案 0 :(得分:0)
因此,我能够通过更改验证网址来解决此问题,即
OpenCV
不适用于Firebase项目,仅适用于在检查reCaptcha for firebase link here
也将我以前的答案更改为:
AWS Cloud
并将其部署到最终成功的firebase上。我认为firebase不允许致电我需要付费计划的较旧的Recaptcha验证地址,但在使用Recaptcha搜索Firebase时解决了我的问题。