Files
calendar/packages/lib/server/checkCfTurnstileToken.ts
T
Dhairyashil ShindeandGitHub b220420630 fix: reset Cloudflare Turnstile token on signup errors (#24927)
* fix: cloudflare turnstile token reset

* fix: silently reset turnstile on invalid token error

* refactor: remove unused forwardRef logic from Turnstile component

- Remove forwardRef, useImperativeHandle, and useRef imports
- Remove unused TurnstileInstance type export
- Simplify to a plain function component
- The ref-based reset was replaced by key-based remount in signup-view

* refactor: remove redundant cfToken validation check

The submit button is already disabled when cfToken is missing,
making this defensive check unreachable during normal form flow.

* revert prettier formatiing

* chore: revert yarn.lock changes

* refactor(auth): use shared constant for cloudflare token error message

Replace hardcoded "Invalid cloudflare token" string with an exported
constant to prevent silent breakage if the error message changes.
2025-12-11 16:45:01 +05:30

38 lines
1.1 KiB
TypeScript

import { HttpError } from "../http-error";
const TURNSTILE_SECRET_ID = process.env.CLOUDFLARE_TURNSTILE_SECRET;
export const INVALID_CLOUDFLARE_TOKEN_ERROR = "Invalid cloudflare token";
export async function checkCfTurnstileToken({ token, remoteIp }: { token?: string; remoteIp: string }) {
// This means the instance doesn't have turnstile enabled - we skip the check and just return success.
// OR the instance is running in CI so we skip these checks also
if (!TURNSTILE_SECRET_ID || !!process.env.NEXT_PUBLIC_IS_E2E) {
return {
success: true,
};
}
if (!token) {
throw new HttpError({ statusCode: 401, message: "No cloudflare token - please try again" });
}
const form = new URLSearchParams();
form.append("secret", TURNSTILE_SECRET_ID);
form.append("response", token);
form.append("remoteip", remoteIp);
const result = await fetch("https://challenges.cloudflare.com/turnstile/v0/siteverify", {
method: "POST",
body: form,
});
const data = await result.json();
if (!data["success"]) {
throw new HttpError({ statusCode: 401, message: INVALID_CLOUDFLARE_TOKEN_ERROR });
}
return data;
}