1
1
import React from 'react' ;
2
2
import {
3
+ useRef ,
3
4
useMemo ,
4
5
useState ,
5
6
useEffect ,
@@ -18,7 +19,7 @@ enum GoogleRecaptchaError {
18
19
}
19
20
20
21
interface IGoogleReCaptchaProviderProps {
21
- reCaptchaKey ? : string ;
22
+ reCaptchaKey : string ;
22
23
language ?: string ;
23
24
useRecaptchaNet ?: boolean ;
24
25
useEnterprise ?: boolean ;
@@ -28,6 +29,17 @@ interface IGoogleReCaptchaProviderProps {
28
29
async ?: boolean ;
29
30
appendTo ?: 'head' | 'body' ;
30
31
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 ;
31
43
} ;
32
44
children : ReactNode ;
33
45
}
@@ -53,11 +65,14 @@ export function GoogleReCaptchaProvider({
53
65
useRecaptchaNet = false ,
54
66
scriptProps,
55
67
language,
68
+ inlineBadgeId,
69
+ parameters = { } ,
56
70
children
57
71
} : IGoogleReCaptchaProviderProps ) {
58
72
const [ greCaptchaInstance , setGreCaptchaInstance ] = useState < null | {
59
73
execute : Function ;
60
74
} > ( null ) ;
75
+ const clientId = useRef < number | string > ( reCaptchaKey ) ;
61
76
62
77
useEffect ( ( ) => {
63
78
if ( ! reCaptchaKey ) {
@@ -69,6 +84,21 @@ export function GoogleReCaptchaProvider({
69
84
}
70
85
71
86
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
+ } ;
72
102
73
103
const onLoad = ( ) => {
74
104
if ( ! window || ! ( window as any ) . grecaptcha ) {
@@ -93,7 +123,8 @@ export function GoogleReCaptchaProvider({
93
123
} ;
94
124
95
125
injectGoogleReCaptchaScript ( {
96
- reCaptchaKey,
126
+ render : inlineBadgeId ? 'explicit' : reCaptchaKey ,
127
+ onLoadCallbackName,
97
128
useEnterprise,
98
129
useRecaptchaNet,
99
130
scriptProps,
@@ -115,18 +146,17 @@ export function GoogleReCaptchaProvider({
115
146
) ;
116
147
}
117
148
118
- const result = await greCaptchaInstance . execute ( reCaptchaKey , { action } ) ;
119
-
120
- return result ;
149
+ return await greCaptchaInstance . execute ( clientId . current , { action } ) ;
121
150
} ,
122
- [ greCaptchaInstance ]
151
+ [ greCaptchaInstance , clientId ]
123
152
) ;
124
153
125
154
const googleReCaptchaContextValue = useMemo (
126
155
( ) => ( {
127
- executeRecaptcha : greCaptchaInstance ? executeRecaptcha : undefined
156
+ executeRecaptcha : greCaptchaInstance ? executeRecaptcha : undefined ,
157
+ inlineBadgeId,
128
158
} ) ,
129
- [ executeRecaptcha , greCaptchaInstance ]
159
+ [ executeRecaptcha , greCaptchaInstance , inlineBadgeId ]
130
160
) ;
131
161
132
162
return (
0 commit comments