Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAPTCHA recipe needs checking #5776

Closed
sarah11918 opened this issue Dec 13, 2023 · 1 comment · Fixed by #5784
Closed

CAPTCHA recipe needs checking #5776

sarah11918 opened this issue Dec 13, 2023 · 1 comment · Fixed by #5784
Labels
code snippet update Updates a code sample: typo, outdated code etc. good first issue Good for newcomers help - confirm behaviour Walk through the example/issue and confirm this is a general behaviour, or a correct update to make. help wanted Issues looking for someone to run with them! improve documentation Enhance existing documentation (e.g. add an example, improve description)

Comments

@sarah11918
Copy link
Member

📚 Subject area/topic

Recipe: captcha

📋 Page(s) affected (or suggested, for new content)

https://docs.astro.build/en/recipes/captcha/

📋 Description of content that is out-of-date or incorrect

We received two comments on Feedback Fish that some code needs updating. Would love help verifying and if necessary correcting this!

Comment 1:
Hello! I wasn't able to get the recaptcha code on https://docs.astro.build/en/recipes/captcha/ working, but upon searching the error being thrown I found this solution: https://stackoverflow.com/questions/52416002/recaptcha-error-codes-missing-input-response-missing-input-secret-when-v

This is the code for /recaptcha.js I have now. It appears to be functioning but I am very much a junior dev—so take it with a grain of salt.

export const prerender = false;

export async function POST({ request }) {
    const data = await request.json();

    const recaptchaURL = "https://www.google.com/recaptcha/api/siteverify";

    const response = await fetch(recaptchaURL, {
        method: "POST",
        headers: { "Content-Type": "application/x-www-form-urlencoded" },
        body: `secret=${import.meta.env.RECAPTCHA_SECRET}&response=${
            data.recaptcha
        }`,
    });

    const responseData = await response.json();

    console.log("Response: ", JSON.stringify(responseData));

    return new Response(JSON.stringify(responseData), { status: 200 });
}

Comment 2:

Hey there,
I want to point out that Astro docs have some issues with an example:
https://docs.astro.build/en/recipes/captcha/
The example should be updated with the correct code as below:

export async function POST({ request }) {
  const data = await request.json();

  const recaptchaURL = 'https://www.google.com/recaptcha/api/siteverify';

 const stringify = `secret=${requestBody.secret}&response=${requestBody.response}`;
  const response = await fetch(recaptchaURL, {
    method: "POST",
 headers: {
            'content-type': 'application/x-www-form-URL-encoded',
        },
    body: stringify
  });

  const responseData = await response.json();

  return new Response(JSON.stringify(responseData), { status: 200 });
}

🖥️ Reproduction in StackBlitz (if reporting incorrect content or code samples)

No response

@sarah11918 sarah11918 added improve documentation Enhance existing documentation (e.g. add an example, improve description) good first issue Good for newcomers help - confirm behaviour Walk through the example/issue and confirm this is a general behaviour, or a correct update to make. help wanted Issues looking for someone to run with them! code snippet update Updates a code sample: typo, outdated code etc. labels Dec 13, 2023
@mingjunlu
Copy link
Contributor

I've confirmed that there is an error in the verification process when following the instructions in Verify a Captcha.

{
  "success": false,
  "error-codes": ["missing-input-secret"]
}

As suggested in the comments, it seems necessary to include 'Content-Type': 'application/x-www-form-urlencoded' in the headers, and the body format should not be in JSON.

I'll submit a PR to adjust the code snippet on the documentation page 🔜

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
code snippet update Updates a code sample: typo, outdated code etc. good first issue Good for newcomers help - confirm behaviour Walk through the example/issue and confirm this is a general behaviour, or a correct update to make. help wanted Issues looking for someone to run with them! improve documentation Enhance existing documentation (e.g. add an example, improve description)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants