У меня есть следующие коды, которые проверяют Google reCAPTCHA v3 в моей функции Firebase, которая вызвала проблему CORS :
const functions = require('firebase-functions');
const nodemailer = require("nodemailer");
const express = require("express");
const cors = require("cors");
const request = require('request');
const serverApi = express();
api.use(cors({ origin: true }));
function verifyCaptcha(token, returnData) {
// Put your secret key here.
var secretKey = functions.config().recaptcha.secretkey;
var verificationUrl = "https://www.google.com/recaptcha/api/siteverify?secret=" + secretKey + "&response=" + token;
// Note here: External network call to google.com
request(verificationUrl, function (error, response, body) {
body = JSON.parse(body);
// Success will be true or false depending upon captcha validation.
if (!body.success) {
body['status'] = false;
body['errSource'] = "recaptcha";
body['message'] = "Failed to pass captcha verification.";
} else {
body['status'] = true;
body['message'] = "Successfully passed captcha verification!";
};
console.log(`Google returns: ${JSON.stringify(body)}`);
returnData(body);
});
};
api.post("/api/service-name", (req, res) => {
if (!req.body['g-recaptcha-response']) {
return res.send({ "status": false, "errSource": "recaptcha", "message": "Client-side reCAPTCHA token not found." });
};
const recaptchaToken = req.body['g-recaptcha-response'];
verifyCaptcha(recaptchaToken, function (result) {
if (result.status == false) {
return res.send(result);
};
// My business logics here.
});
});
exports.api = functions.https.onRequest(api);
Я заметил, что после удаления Запрос проверки reCAPTCHA v3 в моей функции Firebase, больше никаких проблем CORS для моего локального хоста, чтобы вызвать "/api/service-name"
с использованием $.ajax()
. Это связано с тем, что следующий журнал функции Firebase напомнил мне о «Внешняя сеть недоступна» :
Billing account not configured. External network is not accessible and quotas are severely limited.
Configure billing account to remove these restrictions
Мой вопрос: Есть ли способ заставить мою серверную проверку reCAPTCHA работать, не вызывая этой проблемы CORS, которую можно предотвратить с помощью «Не настроена учетная запись для выставления счетов»? Спасибо!
ОБНОВЛЕНИЕ:
После обнаружения ошибки request()
, которая выполняет проверку, я получаю следующую ошибку:
{errno: "EAI_AGAIN", code: "EAI_AGAIN", syscall: "getaddrinfo", hostname: "www.google.com", host: "www.google.com", …}
Также , после обработки этой ошибки проблема с CORS исчезла, но reCAPTCHA по-прежнему не может быть проверена. Есть идеи, что вызывает это? Еще раз спасибо!