New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

turnstile-next

Package Overview
Dependencies
Maintainers
0
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

turnstile-next

The Cloudflare Turnstile Client Side Rendering for React and NextJS

  • 0.2.2
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
0
decreased by-100%
Maintainers
0
Weekly downloads
 
Created
Source

The Cloudflare Turnstile Client Side Component for React and NextJS

This is a React component that can be used to protect your React and NextJS applications with Cloudflare Access.

Installation

npm install --save turnstile-next

or

yarn add turnstile-next

Usage With NextJS

In your layout or page, import the TurnstileNextContext and use it your page or layout like so:

import TurnstileContext from 'turnstile-next/vercel';

export default function Layout({ children }) {
  return (
    <>
        <div>{children}</div>
        <TurnstileContext />
    </>
  );
}

only cloudflare script tag is included here.

Usage With Vite

In your index.html file, use the cloudflare script tag like so:

<script src="https://challenges.cloudflare.com/turnstile/v0/api.js" defer async></script>

and you can use the TurnstileInput component like so:

import TurnstileInput from 'turnstile-next';

const SITE_KEY = import.meta.env.VITE_SITE_KEY;

export default function MyComponent() {
  
  const onVerify = (token : string) => {
    console.log(token);
  } 

  const onErr = (err : string) => {
    console.log(err);
  }
  
  return (
    <div>
        <TurnstileInput onVerify={onVerify} onErr={onErr} siteKey={SITE_KEY} />
    </div>
  );
}

Usage In Component

import TurnstileInput from 'turnstile-next';

const SITE_KEY = process.env.NEXT_PUBLIC_SITE_KEY;

export default function MyComponent() {
  
  const onVerify = (token : string) => {
    console.log(token);
  } 

  const onErr = (err : string) => {
    console.log(err);
  }
  
  return (
    <>
        <TurnstileInput onVerify={onVerify} onErr={onErr} siteKey={SITE_KEY} />
    </>
  );
}

Usage

The TurnstileInput component accepts a set of properties that are used to configure the Turnstile challenge. Here's a detailed description of each property:

PropertyDescription
siteKey (required)The site key for your Turnstile challenge. This key is obtained from the Turnstile dashboard.
themeThe theme of the challenge. Defaults to "auto".
localeThe locale of the challenge. Defaults to "en".
sizeThe size of the challenge. Defaults to "normal".
fieldNameThe name of the field that will be used to store the token. Defaults to "cf-turnstile-token".
retryIntervalThe interval in milliseconds to retry the challenge. Defaults to 8000.
retryWhether to retry the challenge. Defaults to true.
refreshOnExpiredWhether to refresh the challenge when it expires. Defaults to "auto".
onVerifyA callback that will be called when the challenge is verified.
onErrorA callback that will be called when the challenge fails.
onExpireA callback that will be called when the challenge expires.
onBeforeInteractiveA callback that will be called when the challenge is about to be interactive.
onAfterInteractiveA callback that will be called when the challenge is interactive.
onUnsupportedA callback that will be called when the challenge is unsupported by the user's browser.

For detailed information on each property and its usage, please refer to the Turnstile documentation.

utils

refreshTurnstile

This function can be used to refresh the Turnstile challenge. It accepts a single parameter, options, which is an object that can be used to configure the refresh. Here's a detailed description of each property:

PropertyDescription
classNameThe class name of the Turnstile challenge. Defaults to "cf-turnstile".

example:

import { refreshTurnstile } from 'turnstile-next/utils';

const refresh = () => {
  refreshTurnstile({ className: 'cf-turnstile' });
};

export default function MyComponent() {
  return (
    <>
        <button onClick={refresh}>Refresh</button>
    </>
  );
}

checkWidgetRender

This function can be used to check if the Turnstile challenge has been rendered.

example:

import { useEffect } from 'react';
import { checkWidgetRender } from 'turnstile-next/utils';

export default function MyComponent() {
  useEffect(() => {
    checkWidgetRender();
  }, []);

  return (
    <>
        <div>My Component</div>
    </>
  );
}

Contributing

Contributions are always welcome!

License

MIT

Keywords

FAQs

Package last updated on 19 Aug 2024

Did you know?

Socket

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
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc