Skip to content

Commit

Permalink
Support Turnstile with Pre-Clearance (clearance cookie) (#29)
Browse files Browse the repository at this point in the history
* Turnstile Pre-Clearance

* Update README.md
  • Loading branch information
team-good-io authored Jul 15, 2024
1 parent 80b41bc commit befdfe5
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,17 @@ Turnstile takes the following arguments:

And the following callbacks:

| name | arguments | description |
| ------------------- | --------- | --------------------------------------------------- |
| onVerify | token | called when challenge is passed |
| onLoad | widgetId | called when the widget is loaded |
| onError | error | called when an error occurs |
| onExpire | - | called when the token expires |
| onTimeout | token | called when the challenge expires |
| onAfterInteractive | - | called when the challenge becomes interactive |
| onBeforeInteractive | - | called when the challenge no longer is interactive |
| onUnsupported | - | called when the browser is unsupported by Turnstile |
| name | arguments | description |
| ------------------- | ----------------------------- | --------------------------------------------------- |
| onVerify | token | called when challenge is passed |
| onSuccess | token, preClearanceObtained | called when challenge is passed |
| onLoad | widgetId | called when the widget is loaded |
| onError | error | called when an error occurs |
| onExpire | - | called when the token expires |
| onTimeout | token | called when the challenge expires |
| onAfterInteractive | - | called when the challenge becomes interactive |
| onBeforeInteractive | - | called when the challenge no longer is interactive |
| onUnsupported | - | called when the browser is unsupported by Turnstile |

The callbacks also take an additional `BoundTurnstileObject` which exposes
certain functions of `window.turnstile` which are already bound to the
Expand Down
11 changes: 9 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export default function Turnstile({
execution,
userRef,
onVerify,
onSuccess,
onLoad,
onError,
onExpire,
Expand All @@ -81,6 +82,7 @@ export default function Turnstile({
const ownRef = useRef<HTMLDivElement | null>(null);
const inplaceState = useState<TurnstileCallbacks>({
onVerify,
onSuccess,
onLoad,
onError,
onExpire,
Expand Down Expand Up @@ -123,8 +125,10 @@ export default function Turnstile({
"refresh-expired": refreshExpired,
appearance,
execution,
callback: (token: string) =>
inplaceState.onVerify?.(token, boundTurnstileObject),
callback: (token: string, preClearanceObtained: boolean) => {
inplaceState.onVerify?.(token, boundTurnstileObject);
inplaceState.onSuccess?.(token, preClearanceObtained, boundTurnstileObject);
},
"error-callback": (error?: any) =>
inplaceState.onError?.(error, boundTurnstileObject),
"expired-callback": (token: string) =>
Expand Down Expand Up @@ -165,6 +169,7 @@ export default function Turnstile({
]);
useEffect(() => {
inplaceState.onVerify = onVerify;
inplaceState.onSuccess = onSuccess;
inplaceState.onLoad = onLoad;
inplaceState.onError = onError;
inplaceState.onExpire = onExpire;
Expand All @@ -174,6 +179,7 @@ export default function Turnstile({
inplaceState.onUnsupported = onUnsupported;
}, [
onVerify,
onSuccess,
onLoad,
onError,
onExpire,
Expand Down Expand Up @@ -225,6 +231,7 @@ export interface TurnstileProps extends TurnstileCallbacks {

export interface TurnstileCallbacks {
onVerify?: (token: string, boundTurnstile: BoundTurnstileObject) => void;
onSuccess?: (token: string, preClearanceObtained: boolean, boundTurnstile: BoundTurnstileObject) => void;
onLoad?: (widgetId: string, boundTurnstile: BoundTurnstileObject) => void;
onError?: (
error?: Error | any,
Expand Down

0 comments on commit befdfe5

Please sign in to comment.