Skip to content

Commit

Permalink
fix(Support): fix turnstile verification
Browse files Browse the repository at this point in the history
  • Loading branch information
jakeaturner committed Mar 13, 2024
1 parent e828a86 commit 2395890
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 12 deletions.
1 change: 0 additions & 1 deletion client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
<link rel="icon" type="image/png" sizes="96x96" href="/favicon-96x96.png">
<link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png">
<link rel="manifest" href="/manifest.json">
<script src="https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit" async defer></script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-0P2RT3G484"></script>
<script>
Expand Down
10 changes: 10 additions & 0 deletions client/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"@ckeditor/ckeditor5-react": "^6.2.0",
"@fortawesome/fontawesome-free": "^5.15.4",
"@libretexts/insight-ckeditor5-build": "^1.0.0",
"@marsidev/react-turnstile": "^0.5.3",
"@tailwindcss/typography": "^0.5.10",
"@tanstack/react-query": "^4.36.1",
"@tanstack/react-query-devtools": "^4.36.1",
Expand Down
18 changes: 7 additions & 11 deletions client/src/components/support/CreateTicketFlow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import {
SupportTicketPriorityOptions,
} from "../../utils/supportHelpers";
import { useTypedSelector } from "../../state/hooks";
import { Link, useLocation, useParams } from "react-router-dom";
import { Link, useLocation } from "react-router-dom";
import FileUploader from "../FileUploader";
import TurnstileWidget from "../util/TurnstileWidget";

interface CreateTicketFlowProps {
isLoggedIn: boolean;
Expand Down Expand Up @@ -53,6 +54,7 @@ const CreateTicketFlow: React.FC<CreateTicketFlowProps> = ({ isLoggedIn }) => {
const [autoCapturedURL, setAutoCapturedURL] = useState<boolean>(false);
const [success, setSuccess] = useState(false);
const [files, setFiles] = useState<File[]>([]);
const [turnstileToken, setTurnstileToken] = useState<string>("");
const [challengePassed, setChallengePassed] = useState(false);

useEffect(() => {
Expand All @@ -66,15 +68,9 @@ const CreateTicketFlow: React.FC<CreateTicketFlowProps> = ({ isLoggedIn }) => {

useEffect(() => {
if (isLoggedIn) return;
// @ts-ignore
turnstile.render("#turnstile-container", {
sitekey: "0x4AAAAAAAQ3wOi31ZpBiURp",
theme: "light",
callback: function (token: string) {
verifyTurnstile(token);
},
});
}, [isLoggedIn]);
if (!turnstileToken) return;
verifyTurnstile(turnstileToken);
}, [isLoggedIn, turnstileToken]);

async function verifyTurnstile(token: string) {
try {
Expand Down Expand Up @@ -448,7 +444,7 @@ const CreateTicketFlow: React.FC<CreateTicketFlowProps> = ({ isLoggedIn }) => {
/>
{!isLoggedIn && (
<div className="flex flex-row items-center justify-center mt-8">
<div className="cf-turnstile" id="turnstile-container"></div>
<TurnstileWidget onSuccess={(t) => setTurnstileToken(t)} />
</div>
)}
<div className="flex flex-row justify-end mt-4">
Expand Down
13 changes: 13 additions & 0 deletions client/src/components/util/TurnstileWidget.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Turnstile } from "@marsidev/react-turnstile";

interface TurnstileWidgetProps {
onSuccess: (token: string) => void;
}

const TurnstileWidget = (props: TurnstileWidgetProps) => {
return (
<Turnstile siteKey="0x4AAAAAAAQ3wOi31ZpBiURp" onSuccess={props.onSuccess} />
);
};

export default TurnstileWidget;

0 comments on commit 2395890

Please sign in to comment.