Cloudfare Turnstile, A New Way To Prove You Are Real

Comments 0

Share to social media

    This article describes the recommended way to integrate Cloudflare Turnstile into a React application.

When I first heard about Cloudflare Turnstile, I remembered the New York City subway entrances. After all, I have been commuting with the subway for over ten years and can’t even count how many turnstiles there would ever be.

CAPTCHAs are probably the most hated things on the internet. How often were you tired of clicking those traffic lights and motorcycles? I know I hate them. I am almost sure that you do, too. Even though we all know this, as developers, we sometimes have to show CAPTCHA in front of a user. We need our applications to be secure and keep the bots away, right? We didn’t have any better alternatives until just recently.

A single CAPTCHA takes 32 seconds to complete on average. Given 4.6 Billion global internet users see a CAPTCHA every ten days, would equate to 500 human years wasted every single day. This madness has to stop. Cloudflare can reduce the average verification time to about a second. A substantial reduction from 32 seconds

Cloudflare Turnstile to rescue! Drum roll, please 🥁🥁🥁

Cloudflare Turnstile is a free, tool to replace the dreaded CAPTCHAs. Why is the hype? How are they different? Let’s dive a little deeper.

In order to distinguish bots from humans, turnstile checks for typical human behavior in the browser by running non-interactive javascript challenges. Turnstile uses the results of these challenges to make its judgement to identify bots from humans.

How would this help us, the ReactJS developers? Let’s dive into that.

Turnstile in ReactJS and NextJS

This Turnstile team has released different integration libraries for popular front-end frameworks. For ReactJS, the recommended approach from Cloudflare is to use open-source libraries such as react-turnstile.

turnstile_action

Turnstile is a powerful tool for production websites. Since for the rest of the blog, we need an easy ReactJS framework to deploy a test website and turn on Turnstile; we will be using NextJS. Don’t worry; Turnstile works equally well on vanilla ReactJS as well; the addition of NextJS is only for easy deployment to Vercel, nothing more.

Create a React App with NextJS.

This is a standard npx command you can run from your Terminal (If you have VS Code, use its Terminal). Make sure you are in the folder you want your application to be housed in before running this command. For our purposes, you will likely want to make sure you are running this command in the root directory for your github configuration (certainly for the easiest setup. This will create a directory that matches your project name that you will be checking in.

Choosing the default can’t be wrong here. After all, we are focused on Turnstile, not NextJS..

A screenshot of a computer program

Description automatically generated

Please see the highlighted ‘project name’ in the picture above. npx command will create a folder with this name. Make sure to move to that folder using the below command cd .\turnstile-react-nextjs\. This will be the root folder of your newly created application.

Test your app quickly using npx next dev in the context of the base folder for the project (In above example, it is C:\Code\turnstile-react-nextjs>)

Once the command finishes executing, you should see something very similar to the next images when you go to http://localhost:3000 in your browser.

home-default

Deploy our new NextJS application to a public domain with Vercel

We need an actual website to create a siteKey for Cloudflare (We need this for complete configuration). The best bet for us is Vercel. It is from the same team as NextJS, and basic deployments to a public vercel subdomain are free. So, how about we deploy the website we just built?

We will not be getting to the nitty-gritty of Vercel deployments, but we will use the seamless integration by Vercel to Github to connect Vercel to our codebase. Connected a Github repository to Vercel is available on Vercel’s home page after logging in using Github credentials.

A screenshot of a computer

Description automatically generated

  • Go to https://vercel.com/new (Create a new Vercel account or log in with your existing credentials when prompted).
  • Vercel should have permissions to access the repository. To provide access, choose “Adjust Github App Permissions ->” link. This will allow you to connect Vercel to your repo. On the configuration pop-up, Select your new repository under ‘Repository access’ and click ‘Save’
  • Now, using the ‘import’ button, import your repo into Vercel as shown below. With this step, you are ready to deploy.

vercel-github-repo-select

  • Connect the Github repo

vercel-github-repo-connect

  • The Deploy Step – Select ‘Netx.js’ from the Framework Preset dropdown, then click ‘Deploy’ and…

vercel-deploy

  • That’s it! – Now, if we push the code to our Github repo, Vercel will take care of deploying it. Auto-magically!

vercel-deployed

  • Our website is up Look at the URL!

website

Cloudflare Turnstile Configurations

It is time to configure Turnstile and generate a siteKey. We will later marry this siteKey to the web app we just deployed.

As the first step, log in to Cloudflare.com. Select Turnstile from the left navigation menu on your dashboard and click the ‘Add site’ button.

turnstile-addsite

The next screen is where we connect the web app we just deployed with Turnstile. Enter our website’s domain/subdomain name(Did you see? This is where we let Turnstile know who we are and we are real) and press ‘Create’.

Copy the Site Key and Secret Key from the generated screen. We need these keys later.

add-site

Setup Turnstile in our NextJS/React App.

Install Turnstile

The Turnstile team recommends two major packages @marsidev/react-turnstile and react-turnstile. Both have similar configurations. For this article, we will use @marsidev/react-turnstile.

Install the package like this.

Add turnstile into a form.

Now that Turnstile is installed let’s protect a login form using that. First, let’s remove all the content of src\app\page.tsx and insert the following code. After inserting the code, replace the Site Key from Cloudfare in the locations in the code where it is requested.

Now, just push the changes to your git repository. Vercel should take care of auto-deployment. Check your site after deployment, Viola! The turnstile is set up and ready to go.

turnstile-in-action

The styling of the buttons and inputs is done through some quick CSS. A link to this entire project on GitHub is available towards the end of this article.

What is in the source code

The source code is quite self-explanatory, yet we will go through some of the corners.

We are importing Turnstile here.

We ensure the login button stays disabled until Turnstile gives us a green signal.

Here, we are keeping a ref to the Turnstile widget and asking it to reset after we submit a form.

Turnstile is configurable to your needs of language, size, and theme. What if you need a smaller ‘dark’ themed widget in French? You got it with the code below. You can do many more fine-tuning on this widget for your needs.

turnstile_action_french

Now, that’s it. You have built a react + next web app, deployed it using Vercel, connected our website with Cloudflare Turnstile, and finally wrote some code to ensure it all works. I have uploaded the source code for this article here. Clone it, change the siteKey, and you are good to go.

Wrap up

Cloudflare Turnstile is a powerful tool that helps us secure web applications without making our users pull all their hair. It is in the news that bots solve CAPTCHAs better than humans lately. Turnstile is here to stay. Packages like react-turnstile wrap away much of the implementation details and help us integrate Turnstile into our code with few lines. Welcome to a new world without a million traffic lights!

About the author

Aneesh Lal Gopalakrishnan

See Profile

Aneesh Lal has pioneered a variety of technologies over the last two decades. He authored libraries and apps used by millions of customers and developers worldwide, speaks at developer events, and works with coding academies and open source foundations. Follow him on Twitter: https://twitter.com/aneeshlalga

Annesh's contributions