Add turnstile error codes

This commit is contained in:
William Oldham
2023-12-17 20:59:28 +00:00
parent 8f8c472474
commit 631807b511

View File

@@ -90,7 +90,10 @@ async function validateTurnstile(context: Context<Env>) {
});
const outcome = await result.json<any>();
return outcome.success;
return {
success: outcome.success as boolean,
errorCodes: outcome['error-codes'] as string[],
};
}
app.get('/scrape', async (context) => {
@@ -99,11 +102,15 @@ app.get('/scrape', async (context) => {
const turnstileEnabled = Boolean(context.env?.TURNSTILE_ENABLED);
if (turnstileEnabled) {
const success = await validateTurnstile(context);
const turnstileResponse = await validateTurnstile(context);
if (!success) {
if (!turnstileResponse.success) {
context.status(401);
return context.text('Turnstile invalid');
return context.text(
`Turnstile invalid, error codes: ${turnstileResponse.errorCodes.join(
', ',
)}`,
);
}
}