Skip to content

Commit

Permalink
Revert "Moved verification logic to utility"
Browse files Browse the repository at this point in the history
This reverts commit 2bbf00f.
  • Loading branch information
StephenJLu committed Dec 25, 2024
1 parent b909288 commit c293eb0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 32 deletions.
19 changes: 15 additions & 4 deletions app/routes/contact/contact.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,20 @@ export async function action ({ request, context }: { request: Request, context:
try {

const verificationResult = await verifyTurnstileToken(token);
if (verificationResult.errors) {
return json({ errors: verificationResult.errors }, { status: verificationResult.status });
}

if ('status' in verificationResult) {
return json<ActionData>(
{ errors: { message: verificationResult.message } },
{ status: verificationResult.status }
);
}

if (!verificationResult.success) {
return json<ActionData>(
{ errors: { message: 'CAPTCHA verification failed. Please try again.' } },
{ status: 400 }
);
}

const response = await fetch(sendLayerEndpoint, {
method: 'POST',
Expand Down Expand Up @@ -275,7 +286,7 @@ export const Contact = () => {
</div>
)}
</Transition>
{/* Turnstile widget*/}
{/* Turnstile widget, sets WidgetId */}
<Turnstile
className={styles.turnstile}
theme="dark"
Expand Down
40 changes: 12 additions & 28 deletions app/utils/turnstile.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
/*
Turnstile token verification utility function
*/

interface TurnstileResponse {
success: boolean;
'error-codes'?: string[];
Expand All @@ -8,14 +12,12 @@ interface TurnstileError {
status: number;
}

interface ActionData {
errors?: { message: string };
status?: number;
}

export async function verifyTurnstileToken(token: string): Promise<ActionData> {
export async function verifyTurnstileToken(token: string): Promise<TurnstileResponse | TurnstileError> {
try {

/* Worker URL for Turnstile verification */
const workerUrl = 'https://turnstile.stephenjlu.com';

const verificationResponse = await fetch(workerUrl, {
method: 'POST',
headers: {
Expand All @@ -26,33 +28,15 @@ export async function verifyTurnstileToken(token: string): Promise<ActionData> {

const contentType = verificationResponse.headers.get('Content-Type') || '';
if (!contentType.includes('application/json')) {
return {
errors: { message: `Expected JSON response but received: ${contentType}` },
status: 400
};
}

const result = await verificationResponse.json();

if ('status' in result) {
return {
errors: { message: result.message },
status: result.status
};
}

if (!result.success) {
return {
errors: { message: 'CAPTCHA verification failed. Please try again.' },
status: 400
};
throw new Error(`Expected JSON response but received: ${contentType}`);
}

return { status: 200 };
const verificationResult = await verificationResponse.json();
return verificationResult;
} catch (error) {
console.error('Error verifying Turnstile token:', error);
return {
errors: { message: 'An error occurred during CAPTCHA verification.' },
message: 'An error occurred during CAPTCHA verification.',
status: 500
};
}
Expand Down

0 comments on commit c293eb0

Please sign in to comment.