Skip to content

Instantly share code, notes, and snippets.

@andresclua
Last active February 15, 2024 09:11
Show Gist options
  • Save andresclua/02c8cc73c3a4f7ac1f78468b9e1c6b93 to your computer and use it in GitHub Desktop.
Save andresclua/02c8cc73c3a4f7ac1f78468b9e1c6b93 to your computer and use it in GitHub Desktop.
Recaptcha server example astro
import axios from 'axios';
export const prerender = false;
export const POST = async ({ request }) => {
try {
const requestData = await request.json();
const google_access_token = requestData.google_access_token;
const SECRET_KEY = 'yoursecretkey';
// Make a POST request to Google reCAPTCHA verification API
const response = await axios.post( 'https://www.google.com/recaptcha/api/siteverify',null,
{
params: {
secret: SECRET_KEY,
response: google_access_token,
},
}
);
const responseData = response.data;
// Respond based on the reCAPTCHA verification result
if (responseData.success) {
return new Response(
JSON.stringify({
success: true,
server_response: responseData,
}),
{
status: 200,
headers: { 'Content-Type': 'application/json' },
}
);
} else {
return new Response(
JSON.stringify({
success: false,
error: 'reCAPTCHA verification failed',
server_response: responseData,
}),
{
status: 400,
headers: { 'Content-Type': 'application/json' },
}
);
}
} catch (error) {
return new Response(
JSON.stringify({ error: 'Invalid JSON data' }),
{
status: 400,
headers: { 'Content-Type': 'application/json' },
}
);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment