diff --git a/app/src/routes/+page.svelte b/app/src/routes/+page.svelte index efc7083..2afc644 100644 --- a/app/src/routes/+page.svelte +++ b/app/src/routes/+page.svelte @@ -44,38 +44,44 @@ let formDone = $state(false); let formError = $state(''); - let turnstileToken = $state(''); + let tsWidgetId = $state(null); function openModal(plan = '') { modalPlan = plan; formDone = false; formError = ''; - turnstileToken = ''; + tsWidgetId = null; showModal = true; setTimeout(() => { const ts = (window as any).turnstile; if (!ts) return; const el = document.querySelector('.cf-turnstile'); - if (el && !el.hasChildNodes()) { - ts.render(el, { + if (el) { + el.innerHTML = ''; + tsWidgetId = ts.render(el, { sitekey: PUBLIC_TURNSTILE_SITE_KEY, - theme: 'light', - callback: (t: string) => { turnstileToken = t; } + theme: 'light' }); } - }, 80); + }, 100); } function closeModal() { + const ts = (window as any).turnstile; + if (ts && tsWidgetId !== null) ts.remove(tsWidgetId); + tsWidgetId = null; showModal = false; - (window as any).turnstile?.remove('.cf-turnstile'); } async function submitForm() { if (!formName.trim() || !formEmail.trim()) { formError = 'Bitte Name und E-Mail ausfüllen.'; return; } formSending = true; formError = ''; try { - const token = turnstileToken; + const ts = (window as any).turnstile; + const token = tsWidgetId !== null + ? (ts?.getResponse(tsWidgetId) ?? '') + : (ts?.getResponse() ?? ''); + console.log('Turnstile token length:', token?.length); const res = await fetch('/api/contact', { method: 'POST', headers: { 'Content-Type': 'application/json' },