fix: explicitly load Cloudflare Turnstile
All checks were successful
Tests / Backend - Tests skipped: 8, passed: 74
Backend Coverage #### Project Overview
No changes detected, that affect the code coverage.
* Line Coverage: 78.38% (551/703)
* Lines of Code: 703
#### Quality Gates Summary
No active quality gates.
Tests / Frontend - Lint, Tests et Build passed: 83
Frontend Coverage #### Project Overview
No changes detected, that affect the code coverage.
* Line Coverage: 80.85% (553/684)
* Lines of Code: 684
#### Quality Gates Summary
No active quality gates.
THE-TIP-TOP-MULTIBRANCH/pipeline/head This commit looks good
All checks were successful
Tests / Backend - Tests skipped: 8, passed: 74
Backend Coverage #### Project Overview
No changes detected, that affect the code coverage.
* Line Coverage: 78.38% (551/703)
* Lines of Code: 703
#### Quality Gates Summary
No active quality gates.
Tests / Frontend - Lint, Tests et Build passed: 83
Frontend Coverage #### Project Overview
No changes detected, that affect the code coverage.
* Line Coverage: 80.85% (553/684)
* Lines of Code: 684
#### Quality Gates Summary
No active quality gates.
THE-TIP-TOP-MULTIBRANCH/pipeline/head This commit looks good
This commit is contained in:
@@ -1,19 +1,114 @@
|
||||
import { Turnstile } from '@marsidev/react-turnstile'
|
||||
import { CAPTCHA_ENABLED, TURNSTILE_SITE_KEY } from '../config/captcha'
|
||||
import { useEffect, useRef } from 'react'
|
||||
import { TURNSTILE_SITE_KEY } from '../config/captcha'
|
||||
|
||||
// `ref` expose reset() : un jeton Turnstile n'est valable qu'une fois, il faut
|
||||
// en redemander un après chaque envoi refusé.
|
||||
export default function Captcha({ ref, onToken }) {
|
||||
if (!CAPTCHA_ENABLED) return null
|
||||
const TURNSTILE_SCRIPT_ID = 'cf-turnstile-script'
|
||||
const TURNSTILE_SCRIPT_URL =
|
||||
'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit'
|
||||
|
||||
export default function Captcha({ onToken }) {
|
||||
const containerRef = useRef(null)
|
||||
const widgetIdRef = useRef(null)
|
||||
|
||||
useEffect(() => {
|
||||
if (!TURNSTILE_SITE_KEY || !containerRef.current) return
|
||||
|
||||
let cancelled = false
|
||||
|
||||
const renderWidget = () => {
|
||||
if (
|
||||
cancelled ||
|
||||
!containerRef.current ||
|
||||
!window.turnstile
|
||||
) {
|
||||
return
|
||||
}
|
||||
|
||||
if (widgetIdRef.current !== null) {
|
||||
try {
|
||||
window.turnstile.remove(widgetIdRef.current)
|
||||
} catch {
|
||||
// Rien à faire
|
||||
}
|
||||
|
||||
widgetIdRef.current = null
|
||||
}
|
||||
|
||||
containerRef.current.innerHTML = ''
|
||||
|
||||
widgetIdRef.current = window.turnstile.render(
|
||||
containerRef.current,
|
||||
{
|
||||
sitekey: TURNSTILE_SITE_KEY,
|
||||
language: 'fr',
|
||||
size: 'flexible',
|
||||
|
||||
callback: (token) => {
|
||||
onToken(token)
|
||||
},
|
||||
|
||||
'expired-callback': () => {
|
||||
onToken('')
|
||||
},
|
||||
|
||||
'error-callback': () => {
|
||||
onToken('')
|
||||
},
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
const existingScript = document.getElementById(
|
||||
TURNSTILE_SCRIPT_ID
|
||||
)
|
||||
|
||||
if (window.turnstile) {
|
||||
renderWidget()
|
||||
return
|
||||
}
|
||||
|
||||
const previousCallback = window.onloadTurnstileCallback
|
||||
|
||||
window.onloadTurnstileCallback = () => {
|
||||
previousCallback?.()
|
||||
renderWidget()
|
||||
}
|
||||
|
||||
if (!existingScript) {
|
||||
const script = document.createElement('script')
|
||||
|
||||
script.id = TURNSTILE_SCRIPT_ID
|
||||
script.src = `${TURNSTILE_SCRIPT_URL}&onload=onloadTurnstileCallback`
|
||||
script.async = true
|
||||
script.defer = true
|
||||
|
||||
document.head.appendChild(script)
|
||||
}
|
||||
|
||||
return () => {
|
||||
cancelled = true
|
||||
|
||||
if (
|
||||
widgetIdRef.current !== null &&
|
||||
window.turnstile
|
||||
) {
|
||||
try {
|
||||
window.turnstile.remove(widgetIdRef.current)
|
||||
} catch {
|
||||
// Rien à faire
|
||||
}
|
||||
}
|
||||
|
||||
widgetIdRef.current = null
|
||||
}
|
||||
}, [onToken])
|
||||
|
||||
if (!TURNSTILE_SITE_KEY) return null
|
||||
|
||||
return (
|
||||
<Turnstile
|
||||
ref={ref}
|
||||
siteKey={TURNSTILE_SITE_KEY}
|
||||
options={{ language: 'fr', size: 'flexible' }}
|
||||
onSuccess={onToken}
|
||||
onExpire={() => onToken('')}
|
||||
onError={() => onToken('')}
|
||||
<div
|
||||
ref={containerRef}
|
||||
id="turnstile-captcha"
|
||||
className="w-full min-h-[65px] flex justify-center"
|
||||
/>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user