Socket
Socket
Sign inDemoInstall

react-turnstile

Package Overview
Dependencies
5
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-turnstile

React library for Cloudflare's Turnstile CAPTCHA alternative


Version published
Weekly downloads
20K
decreased by-9.26%
Maintainers
1
Install size
34.1 kB
Created
Weekly downloads
 

Changelog

Source

[1.1.3] - 2024-02-19

Changed

  • React peer dependency version to v16.13.1

Readme

Source

react-turnstile

A very simple React library for Cloudflare Turnstile.

Installation

npm i react-turnstile

Usage

import Turnstile, { useTurnstile } from "react-turnstile";

// ...

function TurnstileWidget() {
  const turnstile = useTurnstile();
  return (
    <Turnstile
      sitekey="1x00000000000000000000AA"
      onVerify={(token) => {
        fetch("/login", {
          method: "POST",
          body: JSON.stringify({ token }),
        }).then((response) => {
          if (!response.ok) turnstile.reset();
        });
      }}
    />
  );
}

Turnstile tokens expire after 5 minutes, to automatically reset the challenge once they expire, set the autoResetOnExpire prop to true or reset the widget yourself using the onExpire callback.

Reducing Layout Shift

The turnstile iframe initially loads as invisible before becoming visible and expanding to the expected widget size.

This causes Layout Shift and reduces your Cumulative Layout Shift score and UX.

This can be fixed with the fixedSize={true} option, which will force the wrapper div to be the specific size of turnstile.

Bound Turnstile Object

The Bound Turnstile Object is given as argument to all callbacks and allows you to call certain window.turnstile functions without having to store the widgetId yourself.

function Component() {
  return (
    <Turnstile
      executution="execute"
      onLoad={(widgetId, bound) => {
        // before:
        window.turnstile.execute(widgetId);
        // now:
        bound.execute();
      }}
    />
  );
}

Documentation

Turnstile takes the following arguments:

nametypedescription
sitekeystringsitekey of your website (REQUIRED)
actionstring-
cDatastring-
themestringone of "light", "dark", "auto"
languagestringoverride the language used by turnstile
tabIndexnumber-
responseFieldbooleancontrols generation of <input /> element
responseFieldNamestringchanges the name of <input /> element
sizestringone of "normal", "compact"
fixedSizebooleanfix the size of the <div /> to reduce layout shift
retrystringone of "auto", "never"
retryIntervalnumberinterval of retries in ms
appearancestringone of "always", "execute", "interaction-only"
executionstringone of "render", "execute"
idstringid of the div
userRefRefcustom react ref for the div
classNamestringpassed to the div
styleobjectpassed to the div

And the following callbacks:

nameargumentsdescription
onVerifytokencalled when challenge is passed
onLoadwidgetIdcalled when the widget is loaded
onErrorerrorcalled when an error occurs
onExpire-called when the token expires
onTimeouttokencalled 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 current widget, so you don't need track the widgetId yourself.

For more details on what each argument does, see the Cloudflare Documentation.

FAQs

Last updated on 19 Feb 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc