Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Read turnstile token directly from input element
  • Loading branch information
regalijan committed Oct 19, 2023
1 parent fe6ff7f commit ce86ed6
Showing 1 changed file with 20 additions and 18 deletions.
38 changes: 20 additions & 18 deletions app/routes/report.tsx
Expand Up @@ -42,7 +42,6 @@ export default function () {
const [supportsRequestStreams, setSupportsRequestStreams] = useState(false);
const toast = useToast();
const [uploading, setUploading] = useState(false);
const [turnstileToken, setTurnstileToken] = useState("");
const fileTypes: { [k: string]: string } = {
gif: "image/gif",
heic: "image/heic",
Expand Down Expand Up @@ -81,16 +80,12 @@ export default function () {
}).headers.has("Content-Type");

return duplexAccessed && !hasContentType;
})()
})(),
);
}, []);

const { logged_in, site_key } = useLoaderData<typeof loader>();

function setToken(token: string) {
setTurnstileToken(token);
}

async function submit() {
const usernames = (
document.getElementById("usernames") as HTMLInputElement
Expand Down Expand Up @@ -124,13 +119,23 @@ export default function () {
title: "Too Many Usernames",
});

if (!logged_in && !turnstileToken)
return toast({
description: "Please complete the captcha and try again",
isClosable: true,
status: "error",
title: "Captcha not completed",
});
let turnstileToken = "";

if (!logged_in) {
const tokenElem = document
.getElementsByName("cf-turnstile-response")
.item(0) as HTMLInputElement;

if (!tokenElem.value)
return toast({
description: "Please complete the captcha and try again",
isClosable: true,
status: "error",
title: "Captcha not completed",
});

turnstileToken = tokenElem.value;
}

const description = (
document.getElementById("description") as HTMLTextAreaElement
Expand All @@ -144,6 +149,7 @@ export default function () {

const submitReq = await fetch("/api/reports/submit", {
body: JSON.stringify({
bypass: false,
description: description || undefined,
files: filelist,
turnstileResponse: logged_in ? undefined : turnstileToken,
Expand Down Expand Up @@ -300,11 +306,7 @@ export default function () {
<Textarea id="description" maxLength={512} />
</FormControl>
<br />
<div
className="cf-turnstile"
data-callback="setToken"
data-sitekey={site_key}
></div>
<div className="cf-turnstile" data-sitekey={site_key}></div>
<br />
<Text>
By submitting this form, you agree to the{" "}
Expand Down

0 comments on commit ce86ed6

Please sign in to comment.