refactor(captcha): simplify interval handling in hook (#10974)
Close #10708 --------- Co-authored-by: Félix Malfait <felix@twenty.com>
This commit is contained in:
@ -1,16 +1,19 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useRecoilValue, useSetRecoilState } from 'recoil';
|
||||
|
||||
import { useRequestFreshCaptchaToken } from '@/captcha/hooks/useRequestFreshCaptchaToken';
|
||||
import { isCaptchaScriptLoadedState } from '@/captcha/states/isCaptchaScriptLoadedState';
|
||||
import { getCaptchaUrlByProvider } from '@/captcha/utils/getCaptchaUrlByProvider';
|
||||
import { captchaState } from '@/client-config/states/captchaState';
|
||||
import { CaptchaDriverType } from '~/generated/graphql';
|
||||
import { isUndefinedOrNull } from '~/utils/isUndefinedOrNull';
|
||||
|
||||
export const CaptchaProviderScriptLoaderEffect = () => {
|
||||
const captcha = useRecoilValue(captchaState);
|
||||
const setIsCaptchaScriptLoaded = useSetRecoilState(
|
||||
isCaptchaScriptLoadedState,
|
||||
);
|
||||
const { requestFreshCaptchaToken } = useRequestFreshCaptchaToken();
|
||||
|
||||
useEffect(() => {
|
||||
if (!captcha?.provider || !captcha.siteKey) {
|
||||
@ -43,6 +46,29 @@ export const CaptchaProviderScriptLoaderEffect = () => {
|
||||
document.body.appendChild(scriptElement);
|
||||
}
|
||||
}, [captcha?.provider, captcha?.siteKey, setIsCaptchaScriptLoaded]);
|
||||
useEffect(() => {
|
||||
if (isUndefinedOrNull(captcha?.provider)) {
|
||||
return;
|
||||
}
|
||||
|
||||
let refreshInterval: NodeJS.Timeout;
|
||||
|
||||
switch (captcha.provider) {
|
||||
case CaptchaDriverType.GoogleRecaptcha:
|
||||
// Google reCAPTCHA tokens expire after 120 seconds, refresh at 110 seconds
|
||||
refreshInterval = setInterval(requestFreshCaptchaToken, 110 * 1000);
|
||||
break;
|
||||
case CaptchaDriverType.Turnstile:
|
||||
// Cloudflare Turnstile tokens expire after 500 seconds, refresh at 480 seconds
|
||||
refreshInterval = setInterval(requestFreshCaptchaToken, 480 * 1000);
|
||||
break;
|
||||
default:
|
||||
// Note: hCaptcha has a callback system for expiration that we're not implementing now
|
||||
return;
|
||||
}
|
||||
|
||||
return () => clearInterval(refreshInterval);
|
||||
}, [captcha?.provider, requestFreshCaptchaToken]);
|
||||
|
||||
return <></>;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user