Read turnstile token directly from input element

This commit is contained in:
regalijan 2023-10-19 16:50:15 -04:00
parent fe6ff7f915
commit ce86ed6e5d
Signed by: regalijan
GPG Key ID: 5D4196DA269EF520

View File

@ -42,7 +42,6 @@ export default function () {
const [supportsRequestStreams, setSupportsRequestStreams] = useState(false); const [supportsRequestStreams, setSupportsRequestStreams] = useState(false);
const toast = useToast(); const toast = useToast();
const [uploading, setUploading] = useState(false); const [uploading, setUploading] = useState(false);
const [turnstileToken, setTurnstileToken] = useState("");
const fileTypes: { [k: string]: string } = { const fileTypes: { [k: string]: string } = {
gif: "image/gif", gif: "image/gif",
heic: "image/heic", heic: "image/heic",
@ -81,16 +80,12 @@ export default function () {
}).headers.has("Content-Type"); }).headers.has("Content-Type");
return duplexAccessed && !hasContentType; return duplexAccessed && !hasContentType;
})() })(),
); );
}, []); }, []);
const { logged_in, site_key } = useLoaderData<typeof loader>(); const { logged_in, site_key } = useLoaderData<typeof loader>();
function setToken(token: string) {
setTurnstileToken(token);
}
async function submit() { async function submit() {
const usernames = ( const usernames = (
document.getElementById("usernames") as HTMLInputElement document.getElementById("usernames") as HTMLInputElement
@ -124,13 +119,23 @@ export default function () {
title: "Too Many Usernames", title: "Too Many Usernames",
}); });
if (!logged_in && !turnstileToken) let turnstileToken = "";
return toast({
description: "Please complete the captcha and try again", if (!logged_in) {
isClosable: true, const tokenElem = document
status: "error", .getElementsByName("cf-turnstile-response")
title: "Captcha not completed", .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 = ( const description = (
document.getElementById("description") as HTMLTextAreaElement document.getElementById("description") as HTMLTextAreaElement
@ -144,6 +149,7 @@ export default function () {
const submitReq = await fetch("/api/reports/submit", { const submitReq = await fetch("/api/reports/submit", {
body: JSON.stringify({ body: JSON.stringify({
bypass: false,
description: description || undefined, description: description || undefined,
files: filelist, files: filelist,
turnstileResponse: logged_in ? undefined : turnstileToken, turnstileResponse: logged_in ? undefined : turnstileToken,
@ -300,11 +306,7 @@ export default function () {
<Textarea id="description" maxLength={512} /> <Textarea id="description" maxLength={512} />
</FormControl> </FormControl>
<br /> <br />
<div <div className="cf-turnstile" data-sitekey={site_key}></div>
className="cf-turnstile"
data-callback="setToken"
data-sitekey={site_key}
></div>
<br /> <br />
<Text> <Text>
By submitting this form, you agree to the{" "} By submitting this form, you agree to the{" "}