From a33c696a423be19865667a1a9b5fd300bb173af7 Mon Sep 17 00:00:00 2001 From: hyusap Date: Fri, 13 Dec 2024 18:17:39 -0800 Subject: [PATCH 1/3] middlewarified --- www/.env.template | 3 ++ www/app/Chat.tsx | 92 ++++++++++++++++++++++++---------------- www/app/captcha/page.tsx | 46 ++++++++++++++++++++ www/middleware.ts | 27 ++++++++++-- www/package.json | 2 + www/pnpm-lock.yaml | 34 +++++++++++++-- www/utils/turnstile.ts | 16 +++++++ 7 files changed, 177 insertions(+), 43 deletions(-) create mode 100644 www/app/captcha/page.tsx create mode 100644 www/utils/turnstile.ts diff --git a/www/.env.template b/www/.env.template index a60543b..f443d01 100644 --- a/www/.env.template +++ b/www/.env.template @@ -16,6 +16,9 @@ NEXT_PUBLIC_STRIPE_ENABLED=false STRIPE_SECRET_KEY= NEXT_PUBLIC_STRIPE_PUBLISHABLE_KEY= STRIPE_WEBHOOK_SECRET= +# Turnstile (optional) +NEXT_PUBLIC_TURNSTILE_SITE_KEY= +TURNSTILE_SECRET_KEY= # Agent AI_API_KEY= MODEL= diff --git a/www/app/Chat.tsx b/www/app/Chat.tsx index 0e6188e..69c25a1 100644 --- a/www/app/Chat.tsx +++ b/www/app/Chat.tsx @@ -9,6 +9,7 @@ import Swal from 'sweetalert2'; import { useRef, useEffect, useState, ElementRef, useMemo } from 'react'; import { useRouter } from 'next/navigation'; import { usePostHog } from 'posthog-js/react'; +import Turnstile, { useTurnstile } from 'react-turnstile'; import { createClient } from '@/utils/supabase/client'; import { Reaction } from '@/components/messagebox'; @@ -129,6 +130,10 @@ export default function Chat({ const messageContainerRef = useRef>(null); const [, scrollToBottom] = useAutoScroll(messageContainerRef); + const turnstileEnabled = process.env.NEXT_PUBLIC_TURNSTILE_SITE_KEY !== null; + const turnstileSiteKey = process.env.NEXT_PUBLIC_TURNSTILE_SITE_KEY || ''; + const turnstile = useTurnstile(); + useEffect(() => { if (typeof window !== 'undefined') { posthog?.identify(initialUserId, { email: initialEmail }); @@ -318,7 +323,7 @@ export default function Chat({ honchoResponse ).json()) as HonchoResponse; - const pureThought = thoughtText + const pureThought = thoughtText; thoughtText += '\n\nHoncho Dialectic Response:\n\n' + honchoContent.content; @@ -491,10 +496,10 @@ export default function Chat({ /> )} -
+
{ e.preventDefault(); if (canSend && input.current?.value && canUseApp) { @@ -503,41 +508,56 @@ export default function Chat({ } }} > -