Skip to content

Commit befdfe5

Browse files
authored
Support Turnstile with Pre-Clearance (clearance cookie) (#29)
* Turnstile Pre-Clearance * Update README.md
1 parent 80b41bc commit befdfe5

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,17 @@ Turnstile takes the following arguments:
9696

9797
And the following callbacks:
9898

99-
| name | arguments | description |
100-
| ------------------- | --------- | --------------------------------------------------- |
101-
| onVerify | token | called when challenge is passed |
102-
| onLoad | widgetId | called when the widget is loaded |
103-
| onError | error | called when an error occurs |
104-
| onExpire | - | called when the token expires |
105-
| onTimeout | token | called when the challenge expires |
106-
| onAfterInteractive | - | called when the challenge becomes interactive |
107-
| onBeforeInteractive | - | called when the challenge no longer is interactive |
108-
| onUnsupported | - | called when the browser is unsupported by Turnstile |
99+
| name | arguments | description |
100+
| ------------------- | ----------------------------- | --------------------------------------------------- |
101+
| onVerify | token | called when challenge is passed |
102+
| onSuccess | token, preClearanceObtained | called when challenge is passed |
103+
| onLoad | widgetId | called when the widget is loaded |
104+
| onError | error | called when an error occurs |
105+
| onExpire | - | called when the token expires |
106+
| onTimeout | token | called when the challenge expires |
107+
| onAfterInteractive | - | called when the challenge becomes interactive |
108+
| onBeforeInteractive | - | called when the challenge no longer is interactive |
109+
| onUnsupported | - | called when the browser is unsupported by Turnstile |
109110

110111
The callbacks also take an additional `BoundTurnstileObject` which exposes
111112
certain functions of `window.turnstile` which are already bound to the

src/index.tsx

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export default function Turnstile({
7070
execution,
7171
userRef,
7272
onVerify,
73+
onSuccess,
7374
onLoad,
7475
onError,
7576
onExpire,
@@ -81,6 +82,7 @@ export default function Turnstile({
8182
const ownRef = useRef<HTMLDivElement | null>(null);
8283
const inplaceState = useState<TurnstileCallbacks>({
8384
onVerify,
85+
onSuccess,
8486
onLoad,
8587
onError,
8688
onExpire,
@@ -123,8 +125,10 @@ export default function Turnstile({
123125
"refresh-expired": refreshExpired,
124126
appearance,
125127
execution,
126-
callback: (token: string) =>
127-
inplaceState.onVerify?.(token, boundTurnstileObject),
128+
callback: (token: string, preClearanceObtained: boolean) => {
129+
inplaceState.onVerify?.(token, boundTurnstileObject);
130+
inplaceState.onSuccess?.(token, preClearanceObtained, boundTurnstileObject);
131+
},
128132
"error-callback": (error?: any) =>
129133
inplaceState.onError?.(error, boundTurnstileObject),
130134
"expired-callback": (token: string) =>
@@ -165,6 +169,7 @@ export default function Turnstile({
165169
]);
166170
useEffect(() => {
167171
inplaceState.onVerify = onVerify;
172+
inplaceState.onSuccess = onSuccess;
168173
inplaceState.onLoad = onLoad;
169174
inplaceState.onError = onError;
170175
inplaceState.onExpire = onExpire;
@@ -174,6 +179,7 @@ export default function Turnstile({
174179
inplaceState.onUnsupported = onUnsupported;
175180
}, [
176181
onVerify,
182+
onSuccess,
177183
onLoad,
178184
onError,
179185
onExpire,
@@ -225,6 +231,7 @@ export interface TurnstileProps extends TurnstileCallbacks {
225231

226232
export interface TurnstileCallbacks {
227233
onVerify?: (token: string, boundTurnstile: BoundTurnstileObject) => void;
234+
onSuccess?: (token: string, preClearanceObtained: boolean, boundTurnstile: BoundTurnstileObject) => void;
228235
onLoad?: (widgetId: string, boundTurnstile: BoundTurnstileObject) => void;
229236
onError?: (
230237
error?: Error | any,

0 commit comments

Comments
 (0)