generated from openedx/frontend-template-application
-
Couldn't load subscription status.
- Fork 2
feat: add reCAPTCHA #93
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
Open
brobro10000
wants to merge
9
commits into
main
Choose a base branch
from
hu/ent-10991
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6c4ae3c
feat: add Recaptcha
brobro10000 785ee80
chore: refactor recaptcha implementation
brobro10000 751d1a6
chore: normalize tests
brobro10000 137635a
chore: add test
brobro10000 424b71d
Merge branch 'main' into hu/ent-10991
brobro10000 68087b6
chore: rename env variable to existing name
brobro10000 4110192
chore: linting errors
brobro10000 426b7ff
chore: AI feedback
brobro10000 b218741
chore: cleanup logic
brobro10000 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,8 +1,19 @@ | ||
| import { getConfig } from '@edx/frontend-platform/config'; | ||
| import { ReactElement } from 'react'; | ||
| import { GoogleReCaptchaProvider } from 'react-google-recaptcha-v3'; | ||
|
|
||
| import PlanDetailsPage from '@/components/plan-details-pages/PlanDetailsPage'; | ||
|
|
||
| // TODO: unnecessary layer of abstraction, just move component logic into this file. | ||
| const PlanDetails: React.FC = () => ( | ||
| <PlanDetailsPage /> | ||
| ); | ||
| const PlanDetails = (): ReactElement => { | ||
| const { RECAPTCHA_SITE_KEY_WEB } = getConfig(); | ||
| return ( | ||
| <GoogleReCaptchaProvider | ||
| reCaptchaKey={RECAPTCHA_SITE_KEY_WEB} | ||
| useEnterprise | ||
| > | ||
| <PlanDetailsPage /> | ||
| </GoogleReCaptchaProvider> | ||
| ); | ||
| }; | ||
|
|
||
| export default PlanDetails; |
This file contains hidden or 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 hidden or 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 hidden or 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,82 @@ | ||
| import { getConfig } from '@edx/frontend-platform'; | ||
| import { logError, logInfo } from '@edx/frontend-platform/logging'; | ||
| import { useCallback, useMemo } from 'react'; | ||
| import { useGoogleReCaptcha } from 'react-google-recaptcha-v3'; | ||
|
|
||
| export const RECAPTCHA_STATUS = { | ||
| READY: 'ready', | ||
| LOADING: 'loading', | ||
| DISABLED: 'disabled', | ||
| ERRORED: 'error', | ||
| } as const; | ||
| export type RecaptchaStatus = typeof RECAPTCHA_STATUS[keyof typeof RECAPTCHA_STATUS]; | ||
|
|
||
| export const RECAPTCHA_ACTIONS = { | ||
| SUBMIT: 'submit', | ||
| } as const; | ||
| export type KnownRecaptchaAction = typeof RECAPTCHA_ACTIONS[keyof typeof RECAPTCHA_ACTIONS]; | ||
| export type RecaptchaAction = KnownRecaptchaAction | (string & {}); | ||
|
|
||
| const MSG = { | ||
| NOT_READY: (action: RecaptchaAction) => `reCAPTCHA not ready for action: ${action}. Proceeding without token.`, | ||
| TOKEN_FAIL: (action: RecaptchaAction) => `Failed to obtain reCAPTCHA verification token for action: ${action}. | ||
| Please try again or contact support if the issue persists.`, | ||
| EXEC_FAIL: 'Failed to execute reCAPTCHA', | ||
| } as const; | ||
|
|
||
| /** Return type of the hook */ | ||
| export interface UseRecaptchaTokenResult { | ||
| getToken: () => Promise<string | null>; | ||
| status: RecaptchaStatus; | ||
| /** Convenience booleans */ | ||
| isReady: boolean; | ||
| isLoading: boolean; | ||
| } | ||
|
|
||
| const DEFAULT_ACTION: KnownRecaptchaAction = RECAPTCHA_ACTIONS.SUBMIT; | ||
|
|
||
| const useRecaptchaToken = (actionName: RecaptchaAction = DEFAULT_ACTION): UseRecaptchaTokenResult => { | ||
| const { executeRecaptcha } = useGoogleReCaptcha(); | ||
| const { RECAPTCHA_SITE_KEY_WEB } = getConfig(); | ||
|
|
||
| const status: RecaptchaStatus = useMemo(() => { | ||
| if (!RECAPTCHA_SITE_KEY_WEB) { return RECAPTCHA_STATUS.DISABLED; } | ||
| if (!executeRecaptcha) { return RECAPTCHA_STATUS.LOADING; } | ||
| return RECAPTCHA_STATUS.READY; | ||
| }, [RECAPTCHA_SITE_KEY_WEB, executeRecaptcha]); | ||
|
|
||
| const executeWithFallback = useCallback(async () => { | ||
| if (status === RECAPTCHA_STATUS.READY) { | ||
| const token = await executeRecaptcha!(actionName); | ||
| if (!token) { | ||
| throw new Error(MSG.TOKEN_FAIL(actionName)); | ||
| } | ||
| return token; | ||
| } | ||
|
|
||
| // Fallback: site key exists but recaptcha not initialized yet, or disabled | ||
| if (status !== RECAPTCHA_STATUS.DISABLED) { | ||
| logInfo(MSG.NOT_READY(actionName)); | ||
| } | ||
| return null; | ||
| }, [status, executeRecaptcha, actionName]); | ||
|
|
||
| const getToken = useCallback(async (): Promise<string | null> => { | ||
| try { | ||
| return await executeWithFallback(); | ||
| } catch (err: unknown) { | ||
| const message = (err as { message?: string })?.message ?? MSG.EXEC_FAIL; | ||
| logError(message); | ||
| return null; | ||
| } | ||
| }, [executeWithFallback]); | ||
|
|
||
| return { | ||
| getToken, | ||
| status, | ||
| isReady: status === RECAPTCHA_STATUS.READY, | ||
| isLoading: status === RECAPTCHA_STATUS.LOADING, | ||
| }; | ||
| }; | ||
|
|
||
| export default useRecaptchaToken; |
This file contains hidden or 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 hidden or 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 hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.