/opt/canhelp/apps/api/src/lib
Edit: /opt/canhelp/apps/api/src/lib/turnstile.ts (741B)
const SECRET = process.env.TURNSTILE_SECRET_KEY ?? ''
/**
* Verifies a Cloudflare Turnstile token.
* Returns true if valid (or if secret is not configured — dev mode).
*/
export async function verifyTurnstile(token: string | undefined, ip?: string): Promise
{
if (!SECRET) return true // skip in dev when key is not set
if (!token) return false
const body = new URLSearchParams({ secret: SECRET, response: token })
if (ip) body.set('remoteip', ip)
try {
const res = await fetch('https://challenges.cloudflare.com/turnstile/v0/siteverify', {
method: 'POST',
body,
})
const data = (await res.json()) as { success: boolean }
return data.success === true
} catch {
return false
}
}