<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Tino</title>
    <description>The latest articles on DEV Community by Tino (@timp).</description>
    <link>https://dev.to/timp</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4157904%2Fdc3424f0-9a70-4330-a681-9171f25adb9f.jpg</url>
      <title>DEV Community: Tino</title>
      <link>https://dev.to/timp</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/timp"/>
    <language>en</language>
    <item>
      <title>Simple Lightweight CAPTCHA Alternative for React &amp; Next.js</title>
      <dc:creator>Tino</dc:creator>
      <pubDate>Fri, 02 Oct 2026 15:47:49 +0000</pubDate>
      <link>https://dev.to/timp/simple-lightweight-captcha-alternative-for-react-nextjs-4i3m</link>
      <guid>https://dev.to/timp/simple-lightweight-captcha-alternative-for-react-nextjs-4i3m</guid>
      <description>&lt;h1&gt;
  
  
  i try built a lightweight alternative captcha for react/nextjs 💅
&lt;/h1&gt;

&lt;p&gt;i recently wanted to add a bit of friction to my forms just to slow down automated bots. i didn't need anything massive—just a simple hurdle to make it a little more "expensive" or annoying for bots to spam.&lt;/p&gt;

&lt;p&gt;so i built &lt;strong&gt;react-math-captcha&lt;/strong&gt;. it's just a lightweight, self-hosted tool that does exactly that without overcomplicating things.&lt;/p&gt;




&lt;h2&gt;
  
  
  the simple goal
&lt;/h2&gt;

&lt;p&gt;this isn't trying to be the most "advanced" security layer on the planet. the goal is just to make it more tedious for basic automated scripts to bypass your forms.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;simple integration:&lt;/strong&gt; easy to drop into existing React/Next.js projects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;no database needed:&lt;/strong&gt; uses encrypted JWE sessions (via &lt;code&gt;jose&lt;/code&gt;), so you don't need Redis or a DB to store captcha state.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ssr ready:&lt;/strong&gt; uses SVGs, so it works perfectly with Next.js and other SSR frameworks.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  how to use it
&lt;/h2&gt;

&lt;p&gt;it's a quick setup if you're already in a React environment.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;set the secret
add this to your &lt;code&gt;.env&lt;/code&gt;. this secret is used to encrypt the &lt;strong&gt;JWE session&lt;/strong&gt;, keeping all captcha states secure without needing a database or Redis.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;CAPTCHA_SECRET&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;at-least-16-characters-please
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. install
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;react-math-captcha
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. add the route (Next.js)
&lt;/h3&gt;

&lt;p&gt;create an API route to act as the "brain" of the captcha. this handler manages the &lt;strong&gt;gatekeeper&lt;/strong&gt; (slider) and generates the &lt;strong&gt;SVG math challenges&lt;/strong&gt; to keep things lightweight on the client.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/api/captcha/route.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;createCaptchaHandler&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-math-captcha/server&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;POST&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createCaptchaHandler&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. use the component
&lt;/h3&gt;

&lt;p&gt;mount the widget in your form. users will be prompted to slide a slider (gatekeeper) before receiving the math challenge.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight tsx"&gt;&lt;code&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use client&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;MathCaptcha&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-math-captcha/react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;register&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./actions&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Page&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;form&lt;/span&gt; &lt;span class="na"&gt;action&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;register&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt; &lt;span class="na"&gt;placeholder&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"email"&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;MathCaptcha&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;submit&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;form&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. verify on the server
&lt;/h3&gt;

&lt;p&gt;finally, verify the token on the server. if the answer is correct, the system provides a &lt;code&gt;verificationToken&lt;/code&gt;. once &lt;code&gt;verifyCaptchaToken&lt;/code&gt; is called, the token is &lt;strong&gt;consumed immediately&lt;/strong&gt; (one-time use) to prevent replay attacks by bots.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// actions.ts&lt;/span&gt;
&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;use server&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;verifyCaptchaToken&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-math-captcha/server&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;register&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formData&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;FormData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;verifyCaptchaToken&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;formData&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;// captcha is valid &amp;amp; token consumed (secure from replay attacks)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// invalid captcha&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;error&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;invalid captcha&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  keeping it simple
&lt;/h2&gt;

&lt;p&gt;at the end of the day, sometimes you just need a small barrier to keep things clean. if you're looking for a simple way to add some bot friction, feel free to check out the repo.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GitHub:&lt;/strong&gt; &lt;a href="https://github.com/tinoimammp/react-math-captcha" rel="noopener noreferrer"&gt;tinoimammp/react-math-captcha&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;hope this helps someone!&lt;/p&gt;

&lt;h1&gt;
  
  
  WebDev #ReactJS #NextJS #OpenSource #GenZTech
&lt;/h1&gt;

</description>
      <category>webdev</category>
      <category>captcha</category>
      <category>react</category>
      <category>nextjs</category>
    </item>
  </channel>
</rss>
