-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding recaptcha_v3 #2031
Merged
vgalatro
merged 16 commits into
arc-themes-release-version-2.3.0
from
ASUB-8193_ReCaptcha_V3
Mar 27, 2024
Merged
Adding recaptcha_v3 #2031
Changes from 3 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
4ea7443
recaptcha_v3
LauraPinilla 7dd5bbb
attending feedback
LauraPinilla 9c37b1a
Merge branch 'arc-themes-release-version-2.3.0' into ASUB-8193_ReCapt…
LauraPinilla 21e8573
attending feedback
LauraPinilla ce3a2da
attending feedback
LauraPinilla 07ccd82
attending feedback
LauraPinilla ead9e95
fixing package-lock.json
LauraPinilla 499cbc4
fixing package-lock
LauraPinilla 1f8ac1f
fixing package-lock
LauraPinilla 4131a9b
fixing lock file
LauraPinilla 2b728c2
fixing test and linting errors
LauraPinilla 3d2daec
Merge branch 'arc-themes-release-version-2.3.0' into ASUB-8193_ReCapt…
LauraPinilla 257dbf6
fixing test
LauraPinilla a02b0cf
add tests
LauraPinilla bbb4714
fixing tests
LauraPinilla eaa8e91
fixing linting errors
LauraPinilla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 34 additions & 49 deletions
83
blocks/identity-block/components/bot-challenge-protection/index.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,49 @@ | ||
import React, { useState, useEffect } from "react"; | ||
import { useFusionContext } from "fusion:context"; | ||
import getProperties from "fusion:properties"; | ||
import getTranslatedPhrases from "fusion:intl"; | ||
import { Paragraph, useIdentity, useSales } from "@wpmedia/arc-themes-components"; | ||
import React from "react"; | ||
import { useIdentity, Paragraph } from "@wpmedia/arc-themes-components"; | ||
import useRecaptcha, { RECAPTCHA_V2, RECAPTCHA_V3 } from "../../utils/useRecaptcha"; | ||
import ReCAPTCHA from "react-google-recaptcha"; | ||
import { GoogleReCaptchaProvider } from "react-google-recaptcha-v3"; | ||
import RecaptchaV3 from "./reCaptchaV3"; | ||
|
||
const BotChallengeProtection = ({ challengeIn, setCaptchaToken, className, captchaError, setCaptchaError }) => { | ||
const { Identity, isInitialized } = useIdentity(); | ||
const { Sales } = useSales(); | ||
const [siteKey, setSiteKey] = useState(); | ||
export const ARCXP_CAPTCHA= "ArcXP_captchaToken" | ||
|
||
const BotChallengeProtection = ({ challengeIn, setCaptchaToken, className, captchaError, setCaptchaError, resetRecaptcha }) => { | ||
|
||
const { isInitialized } = useIdentity(); | ||
const { recaptchaVersion, siteKey, isRecaptchaEnabled } = useRecaptcha(challengeIn); | ||
|
||
const onChange = (value) => { | ||
setCaptchaToken(value); | ||
setCaptchaError(null); | ||
localStorage.setItem('ArcXP_captchaToken', value); | ||
localStorage.setItem(ARCXP_CAPTCHA, value); | ||
}; | ||
|
||
useEffect(() => { | ||
const checkCaptcha = async () => { | ||
const config = await Identity.getConfig(); | ||
const {recaptchaSiteKey, recaptchaScore } = config; | ||
const isIdentityCaptchaEnabled = config?.[`${challengeIn}Recaptcha`]; | ||
|
||
if(['signup', 'signin', 'magicLink'].includes(challengeIn)) { | ||
if (isIdentityCaptchaEnabled && recaptchaScore === '-1' && recaptchaSiteKey) { | ||
setSiteKey(recaptchaSiteKey); | ||
} | ||
} | ||
|
||
if (challengeIn === 'checkout') { | ||
const salesConfig = await Sales.getConfig(); | ||
const isSalesCaptchaEnabled = salesConfig?.checkoutRecaptchaEnabled; | ||
if (isSalesCaptchaEnabled && recaptchaScore === '-1' && recaptchaSiteKey) { | ||
setSiteKey(recaptchaSiteKey); | ||
} | ||
} | ||
|
||
}; | ||
checkCaptcha(); | ||
|
||
}, [Identity, Sales, challengeIn]); | ||
|
||
const { arcSite } = useFusionContext(); | ||
const { locale } = getProperties(arcSite); | ||
const phrases = getTranslatedPhrases(locale); | ||
|
||
if (!isInitialized) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<section className={`${className}__bot-protection-section`} data-testid="bot-challege-protection-container"> | ||
{!!siteKey && <ReCAPTCHA | ||
sitekey={siteKey} | ||
onChange={onChange} | ||
onExpired={() => {}} | ||
/>} | ||
{captchaError && <Paragraph data-testid="bot-challege-captcha-error">{phrases.t("identity-block.bot-protection-error")}</Paragraph>} | ||
</section> | ||
); | ||
if (isRecaptchaEnabled && !!siteKey && !!recaptchaVersion) { | ||
if (recaptchaVersion === RECAPTCHA_V2) { | ||
return ( | ||
<section | ||
className={`${className}__bot-protection-section`} | ||
data-testid="bot-challege-protection-container" | ||
> | ||
<ReCAPTCHA sitekey={siteKey} onChange={onChange} onExpired={() => {}}/> | ||
{captchaError && <Paragraph>{phrases.t("identity-block.bot-protection-error")}</Paragraph>} | ||
</section> | ||
); | ||
} | ||
if (recaptchaVersion === RECAPTCHA_V3) { | ||
return ( | ||
<GoogleReCaptchaProvider reCaptchaKey={siteKey} scriptProps={{ async: true }}> | ||
<RecaptchaV3 setCaptchaToken={setCaptchaToken} resetRecaptcha={resetRecaptcha} /> | ||
</GoogleReCaptchaProvider> | ||
); | ||
} | ||
} else { | ||
return null; | ||
} | ||
}; | ||
|
||
export default BotChallengeProtection; |
25 changes: 25 additions & 0 deletions
25
blocks/identity-block/components/bot-challenge-protection/reCaptchaV3.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { useEffect, useCallback } from "react"; | ||
import { useGoogleReCaptcha } from "react-google-recaptcha-v3"; | ||
import { ARCXP_CAPTCHA } from "./index"; | ||
|
||
const RecaptchaV3 = ({ setCaptchaToken, resetRecaptcha }) => { | ||
const { executeRecaptcha } = useGoogleReCaptcha(); | ||
|
||
const handleReCaptcha3Verify = useCallback(async () => { | ||
if (!executeRecaptcha) { | ||
console.log("ArcXP - Execute recaptcha not yet available"); | ||
return; | ||
} | ||
const token = await executeRecaptcha(); | ||
setCaptchaToken(token); | ||
localStorage.setItem(ARCXP_CAPTCHA, token); | ||
}, [executeRecaptcha]); | ||
|
||
useEffect(() => { | ||
handleReCaptcha3Verify(); | ||
}, [executeRecaptcha, resetRecaptcha]); | ||
|
||
return null; | ||
}; | ||
|
||
export default RecaptchaV3; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what's the purpose of this variable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @edwardcho1231 similar to reCaptchav2, reCaptchav3 also expires, but in order to grab a new token we need to call executeRecaptcha(). Thus, resetRecaptcha is changing from true to false and viceversa everytime the reCaptcha3 is expired, thus the useEffect() detects this change and we need to grab a new one.