Skip to content

Commit a79a9a1

Browse files
author
Ramirez Nicolás
committed
add inline-badge
1 parent 8110bb6 commit a79a9a1

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

src/google-recaptcha-provider.tsx

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import React from 'react';
22
import {
3+
useRef,
34
useMemo,
45
useState,
56
useEffect,
@@ -18,7 +19,7 @@ enum GoogleRecaptchaError {
1819
}
1920

2021
interface IGoogleReCaptchaProviderProps {
21-
reCaptchaKey?: string;
22+
reCaptchaKey: string;
2223
language?: string;
2324
useRecaptchaNet?: boolean;
2425
useEnterprise?: boolean;
@@ -28,6 +29,17 @@ interface IGoogleReCaptchaProviderProps {
2829
async?: boolean;
2930
appendTo?: 'head' | 'body';
3031
id?: string;
32+
onLoadCallbackName?: string;
33+
};
34+
inlineBadgeId?: string | HTMLElement;
35+
parameters?: {
36+
sitekey?: string;
37+
theme?: string;
38+
size?: string;
39+
tabindex?: number;
40+
callback?: () => void;
41+
expiredCallback?: () => void;
42+
errorCallback?: () => void;
3143
};
3244
children: ReactNode;
3345
}
@@ -53,11 +65,14 @@ export function GoogleReCaptchaProvider({
5365
useRecaptchaNet = false,
5466
scriptProps,
5567
language,
68+
inlineBadgeId,
69+
parameters = {},
5670
children
5771
}: IGoogleReCaptchaProviderProps) {
5872
const [greCaptchaInstance, setGreCaptchaInstance] = useState<null | {
5973
execute: Function;
6074
}>(null);
75+
const clientId = useRef<number | string>(reCaptchaKey);
6176

6277
useEffect(() => {
6378
if (!reCaptchaKey) {
@@ -69,6 +84,21 @@ export function GoogleReCaptchaProvider({
6984
}
7085

7186
const scriptId = scriptProps?.id || 'google-recaptcha-v3';
87+
const onLoadCallbackName = scriptProps?.onLoadCallbackName || 'onRecaptchaLoadCallback';
88+
89+
(window as {[key: string]: any})[onLoadCallbackName] = () => {
90+
const grecaptcha = useEnterprise
91+
? (window as any).grecaptcha.enterprise
92+
: (window as any).grecaptcha;
93+
94+
const params = {
95+
badge: 'inline',
96+
size: 'invisible',
97+
sitekey: reCaptchaKey,
98+
...(parameters || {})
99+
};
100+
clientId.current = grecaptcha.render(inlineBadgeId, params);
101+
};
72102

73103
const onLoad = () => {
74104
if (!window || !(window as any).grecaptcha) {
@@ -93,7 +123,8 @@ export function GoogleReCaptchaProvider({
93123
};
94124

95125
injectGoogleReCaptchaScript({
96-
reCaptchaKey,
126+
render: inlineBadgeId ? 'explicit' : reCaptchaKey,
127+
onLoadCallbackName,
97128
useEnterprise,
98129
useRecaptchaNet,
99130
scriptProps,
@@ -115,18 +146,17 @@ export function GoogleReCaptchaProvider({
115146
);
116147
}
117148

118-
const result = await greCaptchaInstance.execute(reCaptchaKey, { action });
119-
120-
return result;
149+
return await greCaptchaInstance.execute(clientId.current, { action });
121150
},
122-
[greCaptchaInstance]
151+
[greCaptchaInstance, clientId]
123152
);
124153

125154
const googleReCaptchaContextValue = useMemo(
126155
() => ({
127-
executeRecaptcha: greCaptchaInstance ? executeRecaptcha : undefined
156+
executeRecaptcha: greCaptchaInstance ? executeRecaptcha : undefined,
157+
inlineBadgeId,
128158
}),
129-
[executeRecaptcha, greCaptchaInstance]
159+
[executeRecaptcha, greCaptchaInstance, inlineBadgeId]
130160
);
131161

132162
return (

src/utils.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
interface InjectGoogleReCaptchaScriptParams {
2-
reCaptchaKey: string;
2+
render: string;
3+
onLoadCallbackName: string;
34
useRecaptchaNet: boolean;
45
useEnterprise: boolean;
56
onLoad: () => void;
@@ -83,7 +84,8 @@ export const cleanGoogleRecaptcha = (scriptId: string) => {
8384
* @returns
8485
*/
8586
export const injectGoogleReCaptchaScript = ({
86-
reCaptchaKey,
87+
render,
88+
onLoadCallbackName,
8789
language,
8890
onLoad,
8991
useRecaptchaNet,
@@ -114,7 +116,9 @@ export const injectGoogleReCaptchaScript = ({
114116
});
115117
const js = document.createElement('script');
116118
js.id = scriptId;
117-
js.src = `${googleRecaptchaSrc}?render=${reCaptchaKey}${
119+
js.src = `${googleRecaptchaSrc}?render=${render}${
120+
render === 'explicit' ? `&onload=${onLoadCallbackName}` : ''
121+
}${
118122
language ? `&hl=${language}` : ''
119123
}`;
120124

0 commit comments

Comments
 (0)