Fix: Turnstile token via getResponse(widgetId), debug log

This commit is contained in:
rene 2026-05-17 20:03:02 +02:00
parent ca30fde467
commit 08efc258a0

View file

@ -44,38 +44,44 @@
let formDone = $state(false); let formDone = $state(false);
let formError = $state(''); let formError = $state('');
let turnstileToken = $state(''); let tsWidgetId = $state<string | null>(null);
function openModal(plan = '') { function openModal(plan = '') {
modalPlan = plan; modalPlan = plan;
formDone = false; formDone = false;
formError = ''; formError = '';
turnstileToken = ''; tsWidgetId = null;
showModal = true; showModal = true;
setTimeout(() => { setTimeout(() => {
const ts = (window as any).turnstile; const ts = (window as any).turnstile;
if (!ts) return; if (!ts) return;
const el = document.querySelector('.cf-turnstile'); const el = document.querySelector('.cf-turnstile');
if (el && !el.hasChildNodes()) { if (el) {
ts.render(el, { el.innerHTML = '';
tsWidgetId = ts.render(el, {
sitekey: PUBLIC_TURNSTILE_SITE_KEY, sitekey: PUBLIC_TURNSTILE_SITE_KEY,
theme: 'light', theme: 'light'
callback: (t: string) => { turnstileToken = t; }
}); });
} }
}, 80); }, 100);
} }
function closeModal() { function closeModal() {
const ts = (window as any).turnstile;
if (ts && tsWidgetId !== null) ts.remove(tsWidgetId);
tsWidgetId = null;
showModal = false; showModal = false;
(window as any).turnstile?.remove('.cf-turnstile');
} }
async function submitForm() { async function submitForm() {
if (!formName.trim() || !formEmail.trim()) { formError = 'Bitte Name und E-Mail ausfüllen.'; return; } if (!formName.trim() || !formEmail.trim()) { formError = 'Bitte Name und E-Mail ausfüllen.'; return; }
formSending = true; formError = ''; formSending = true; formError = '';
try { 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', { const res = await fetch('/api/contact', {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },