Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

✨ Add support for cloudflare turnstile (#2189)

Luke Vellace03da171a75
diff --git a/apps/web/package.json b/apps/web/package.json
index b4f2802b4..5075b05bc 100644
--- a/apps/web/package.json
+++ b/apps/web/package.json
@@ -29,6 +29,7 @@
"@hono/zod-openapi": "^1.2.0",
"@hono/zod-validator": "^0.7.2",
"@hookform/resolvers": "^5.2.2",
+ "@marsidev/react-turnstile": "^1.4.2",
"@next/env": "16.1.6",
"@prisma/adapter-pg": "^7.3.0",
"@prisma/client": "^7.3.0",
diff --git a/apps/web/src/app/[locale]/(auth)/register/components/register-name-form.tsx b/apps/web/src/app/[locale]/(auth)/register/components/register-name-form.tsx
index 7d45564f5..659650946 100644
--- a/apps/web/src/app/[locale]/(auth)/register/components/register-name-form.tsx
+++ b/apps/web/src/app/[locale]/(auth)/register/components/register-name-form.tsx
@@ -1,5 +1,7 @@
"use client";
import { zodResolver } from "@hookform/resolvers/zod";
+import type { TurnstileInstance } from "@marsidev/react-turnstile";
+import { Turnstile } from "@marsidev/react-turnstile";
import { Button } from "@rallly/ui/button";
import {
Form,
@@ -13,8 +15,10 @@ import { Input } from "@rallly/ui/input";
import { PasswordInput } from "@rallly/ui/password-input";
import { absoluteUrl } from "@rallly/utils/absolute-url";
import { useRouter, useSearchParams } from "next/navigation";
+import React from "react";
import { useForm } from "react-hook-form";
import { setVerificationEmail } from "@/app/[locale]/(auth)/login/actions";
+import { env } from "@/env";
import { PasswordStrengthMeter } from "@/features/password/components/password-strength-meter";
import { Trans, useTranslation } from "@/i18n/client";
import { authClient } from "@/lib/auth-client";
@@ -22,11 +26,18 @@ import { getBrowserTimeZone } from "@/utils/date-time-utils";
import { validateRedirectUrl } from "@/utils/redirect";
import { useRegisterNameFormSchema } from "./schema";
+const turnstileSiteKey = env.NEXT_PUBLIC_TURNSTILE_SITE_KEY;
+const isTurnstileEnabled = !!turnstileSiteKey;
+
export function RegisterNameForm() {
const schema = useRegisterNameFormSchema();
const { t, i18n } = useTranslation();
const searchParams = useSearchParams();
const router = useRouter();
+ const turnstileRef = React.useRef<TurnstileInstance>(null);
+ const [turnstileToken, setTurnstileToken] = React.useState<string | null>(
+ null,
+ );
const form = useForm({
defaultValues: {
name: "",
@@ -60,6 +71,13 @@ export function RegisterNameForm() {
timeZone: getBrowserTimeZone(),
locale: i18n.language,
callbackURL: verifyURL,
+ fetchOptions: turnstileToken
+ ? {
+ headers: {
+ "x-captcha-response": turnstileToken,
+ },
+ }
+ : undefined,
});
if (res.error) {
@@ -98,6 +116,11 @@ export function RegisterNameForm() {
});
}
console.error(error);
+ } finally {
+ if (isTurnstileEnabled) {
+ setTurnstileToken(null);
+ turnstileRef.current?.reset();
+ }
}
})}
>
@@ -175,9 +198,31 @@ export function RegisterNameForm() {
{form.formState.errors.root?.message ? (
<FormMessage>{form.formState.errors.root.message}</FormMessage>
) : null}
+ {turnstileSiteKey ? (
+ <div className="mt-6">
+ <Turnstile
+ ref={turnstileRef}
+ siteKey={turnstileSiteKey}
+ options={{
+ language: i18n.language,
+ }}
+ onSuccess={(token) => {
+ setTurnstileToken(token);
+ }}
+ onError={() => {
+ setTurnstileToken(null);
+ }}
+ onExpire={() => {
+ setTurnstileToken(null);
+ turnstileRef.current?.reset();
+ }}
+ />
+ </div>
+ ) : null}
<div className="mt-6">
<Button
loading={form.formState.isSubmitting}
+ disabled={isTurnstileEnabled && !turnstileToken}
className="w-full"
variant="primary"
type="submit"
diff --git a/apps/web/src/env.ts b/apps/web/src/env.ts
index bde1299b0..5eb199a2b 100644
--- a/apps/web/src/env.ts
+++ b/apps/web/src/env.ts
@@ -135,6 +135,11 @@ export const env = createEnv({
KV_REST_API_URL: z.url().optional(),
KV_REST_API_TOKEN: z.string().optional(),
KV_URL: z.url().optional(),
+ /**
+ * Cloudflare Turnstile secret key for bot protection on registration.
+ * If not set, Turnstile verification is disabled.
+ */
+ TURNSTILE_SECRET_KEY: z.string().optional(),
},
/*
* Environment variables available on the client (and server).
@@ -146,6 +151,11 @@ export const env = createEnv({
NEXT_PUBLIC_POSTHOG_API_KEY: z.string().optional(),
NEXT_PUBLIC_POSTHOG_API_HOST: z.url().optional(),
NEXT_PUBLIC_SELF_HOSTED: z.enum(["true", "false"]).optional(),
+ /**
+ * Cloudflare Turnstile site key for bot protection on registration.
+ * If not set, the Turnstile widget is not rendered.
+ */
+ NEXT_PUBLIC_TURNSTILE_SITE_KEY: z.string().optional(),
},
/*
* Due to how Next.js bundles environment variables on Edge and Client,
@@ -213,6 +223,8 @@ export const env = createEnv({
KV_REST_API_URL: process.env.KV_REST_API_URL,
KV_REST_API_TOKEN: process.env.KV_REST_API_TOKEN,
KV_URL: process.env.KV_URL,
+ TURNSTILE_SECRET_KEY: process.env.TURNSTILE_SECRET_KEY,
+ NEXT_PUBLIC_TURNSTILE_SITE_KEY: process.env.NEXT_PUBLIC_TURNSTILE_SITE_KEY,
},
skipValidation: !!process.env.SKIP_ENV_VALIDATION,
});
diff --git a/apps/web/src/lib/auth.ts b/apps/web/src/lib/auth.ts
index 7bc1ad625..8147bae3b 100644
--- a/apps/web/src/lib/auth.ts
+++ b/apps/web/src/lib/auth.ts
@@ -8,6 +8,7 @@ import { prismaAdapter } from "better-auth/adapters/prisma";
import {
admin,
anonymous,
+ captcha,
createAuthMiddleware,
emailOTP,
genericOAuth,
@@ -38,6 +39,16 @@ const logger = createLogger("auth");
const plugins: BetterAuthPlugin[] = [];
+if (env.TURNSTILE_SECRET_KEY) {
+ plugins.push(
+ captcha({
+ provider: "cloudflare-turnstile",
+ secretKey: env.TURNSTILE_SECRET_KEY,
+ endpoints: ["/sign-up/email"],
+ }),
+ );
+}
+
if (env.OIDC_CLIENT_ID && env.OIDC_CLIENT_SECRET && env.OIDC_DISCOVERY_URL) {
if (env.OIDC_ISSUER_URL) {
logger.info(
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 9e7d30e62..5cffdd6af 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -83,7 +83,7 @@ importers:
version: 8.1.0([email protected])
'@vercel/analytics':
specifier: ^1.5.0
dayjs:
specifier: ^1.11.13
version: 1.11.19
@@ -122,7 +122,7 @@ importers:
version: 6.0.0(@types/[email protected])([email protected])
next-seo:
specifier: ^6.1.0
pg:
specifier: ^8.17.1
version: 8.18.0
@@ -211,6 +211,9 @@ importers:
'@hookform/resolvers':
specifier: ^5.2.2
+ '@marsidev/react-turnstile':
+ specifier: ^1.4.2
'@next/env':
specifier: 16.1.6
version: 16.1.6
@@ -2692,6 +2695,12 @@ packages:
'@leichtgewicht/[email protected]':
resolution: {integrity: sha512-Vo+PSpZG2/fmgmiNzYK9qWRh8h/CHrwD0mo1h1DzL4yzHNSfWYujGTYsWGreD000gcgmZ7K4Ys6Tx9TxtsKdDw==}
+ '@marsidev/[email protected]':
+ resolution: {integrity: sha512-xs1qOuyeMOz6t9BXXCXWiukC0/0+48vR08B7uwNdG05wCMnbcNgxiFmdFKDOFbM76qFYFRYlGeRfhfq1U/iZmA==}
+ peerDependencies:
+ react: ^17.0.2 || ^18.0.0 || ^19.0
+ react-dom: ^17.0.2 || ^18.0.0 || ^19.0
+
'@mdx-js/[email protected]':
resolution: {integrity: sha512-f6ZO2ifpwAQIpzGWaBQT2TXxPv6z3RBzQKpVftEWN78Vl/YweF1uwussDx8ECAXVtr3Rs89fKyG9YlzUs9DyGQ==}
@@ -12801,6 +12810,11 @@ snapshots:
'@leichtgewicht/[email protected]': {}
+ dependencies:
+ react: 19.2.4
+ react-dom: 19.2.4([email protected])
+
'@mdx-js/[email protected]':
dependencies:
'@types/estree': 1.0.8
@@ -15996,7 +16010,7 @@ snapshots:
dependencies:
uncrypto: 0.1.3
optionalDependencies:
react: 19.2.4
@@ -19728,7 +19742,7 @@ snapshots:
react: 19.2.4
react-dom: 19.2.4([email protected])
dependencies:
react: 19.2.4