npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

turnstile-next

v0.2.2

Published

The Cloudflare Turnstile Client Side Rendering for React and NextJS

Downloads

632

Readme

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:

| Property | Description | | -------------------- | --------------------------------------------------------------------------------------------------- | | siteKey (required) | The site key for your Turnstile challenge. This key is obtained from the Turnstile dashboard. | | theme | The theme of the challenge. Defaults to "auto". | | locale | The locale of the challenge. Defaults to "en". | | size | The size of the challenge. Defaults to "normal". | | fieldName | The name of the field that will be used to store the token. Defaults to "cf-turnstile-token". | | retryInterval | The interval in milliseconds to retry the challenge. Defaults to 8000. | | retry | Whether to retry the challenge. Defaults to true. | | refreshOnExpired | Whether to refresh the challenge when it expires. Defaults to "auto". | | onVerify | A callback that will be called when the challenge is verified. | | onError | A callback that will be called when the challenge fails. | | onExpire | A callback that will be called when the challenge expires. | | onBeforeInteractive | A callback that will be called when the challenge is about to be interactive. | | onAfterInteractive | A callback that will be called when the challenge is interactive. | | onUnsupported | A 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:

| Property | Description | | -------------------- | --------------------------------------------------------------------------------------------------- | | className | The 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