1

I am attempting to implement google's v3 recaptcha. Here is my front end ...

  const submitItem = (values) => {
    window.grecaptcha.ready(() => {
      window.grecaptcha
        .execute(SITE_KEY, { action: "submit" })
        .then((token) => {
          axios({
            method: "POST",
            url: `${myEndpoint}/ballot`,
            data: {
              item: { email: values.email },
              itempieces: pieces,
              "g-recaptcha-response": token
            }
          })
            .then((res) => {
              clearItems();
            })
            .catch((e) => {
              console.log("error submitting items", e);
              setAlertText(
                "It looks like something went wrong. Please try to submit again."
              );
            });
        });
    });
  };

I can confirm that a captcha token is sent. Here is the back end ...

const createItem = async (req, res) => {
  try {
    let tempItem = req.body.item;
    let itempieces = req.body.itempieces;
    const captcha = req.body["g-recaptcha-response"];
    var VERIFY_URL = `https://www.google.com/recaptcha/api/siteverify?secret=${CAPTCHA_SECRET_KEY}&response=${captcha}`;
    const google_res = await fetch(VERIFY_URL, { method: "POST" });
    if (google_res.success) {
      const item = await models.ballot.create({
        ...tempItem,
        score: google_res.score,
      });
      res.status(201).json({ ...item, google: google_res });
    } else {
      res.status(201).json(google_res);
    }
  } catch (error) {
    console.log(error);
    res.status(400).json({ error });
  }
};

The google_res.success appears to never be true. All that is returned in google_res is the following object.

size: 0
timeout: 0

I believe that the captcha is working, I expected it to return an object with a score ...?

Joshua Foxworth
  • 1,236
  • 1
  • 22
  • 50

1 Answers1

1

OK. So I am not with it this morning.

I forgot that I need to add a second await command ...

const google_fetch = await fetch(VERIFY_URL, { method: "POST" });
const google_res = await google_fetch.text();
if (google_res.success) {
Joshua Foxworth
  • 1,236
  • 1
  • 22
  • 50
  • This works. Do you have any official doc reference for this `.text();` method in `google_fetch`? – Ram Jan 18 '23 at 14:05