2024-06-28 05:54:32 -04:00
|
|
|
import { useRef } from 'react'
|
2023-06-26 04:32:49 -04:00
|
|
|
import ReCAPTCHA from 'react-google-recaptcha'
|
|
|
|
|
|
|
|
export const useRecaptcha = () => {
|
2024-06-28 05:54:32 -04:00
|
|
|
const ref = useRef<ReCAPTCHA | null>(null)
|
|
|
|
|
|
|
|
const getReCaptchaToken = async (): Promise<
|
|
|
|
ReturnType<ReCAPTCHA['executeAsync']>
|
|
|
|
> => {
|
2023-06-26 04:32:49 -04:00
|
|
|
if (!ref.current) {
|
|
|
|
return null
|
|
|
|
}
|
2024-06-28 05:54:32 -04:00
|
|
|
// Reset the reCAPTCHA before each submission.
|
|
|
|
// The reCAPTCHA token is meant to be used once per validation
|
|
|
|
ref.current.reset()
|
2023-06-26 04:32:49 -04:00
|
|
|
return await ref.current.executeAsync()
|
|
|
|
}
|
2024-06-28 05:54:32 -04:00
|
|
|
|
2023-06-26 04:32:49 -04:00
|
|
|
return { ref, getReCaptchaToken }
|
|
|
|
}
|