Honeypot Spam Protection - A Simpler Alternative to CAPTCHA

Today, we are going to learn about an interesting concept called "honeypot" spam protection. This is a fantastic alternative for those of you who find the implementation of CAPTCHA quite complex and time-consuming.

What is Honeypot Spam Protection?

A honeypot is a security mechanism that works by deceiving spammers into believing they have found a vulnerability to exploit. In reality, it's a trap set up by developers to catch them in the act. It's like leaving out honey for bears - hence the name 'honeypot'.

In terms of web development, this often takes form as invisible fields on forms that humans wouldn't fill out but bots would.

Now let's dive into how we can implement this in our React TypeScript application!

Implementation Example

Firstly, install react-hook-form via npm:

npm install react-hook-form

Next, create your form component:

import { useForm } from 'react-hook-form';

type FormData = {
  email: string;
  password: string;
  honeypot: string; // This field will be hidden.
};

export default function App() {
  const { register, handleSubmit } = useForm<FormData>();

  const onSubmit = (data: FormData) => {
    if (data.honeypot.length !== 0) return; // If honeypot field has any content then it must be filled by bot.
    
    console.log(data);
    // Proceed with rest of your logic here...
  };

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input {...register("email")} placeholder="Email" />
      <input {...register("password")} placeholder="Password" type="password"/>
      
      {/* The honeypot */}
      <div style={{ display: 'none' }}>
        <input {...register("honeypot")} 
	        placeholder="Leave this field blank"
	        tabIndex={-1} // Prevents tabbing into this element.
			aria-hidden="true" // Indicates this element should be hidden 
			/>
      </div>
      
      <button type="submit">Submit</button>
    </form>
  );
}

In the above code, we've created a form with three fields - email, password and honeypot. The honeypot field is hidden from human users by setting its display style to 'none'. Bots, however, will still "see" this field and fill it out.

When the form is submitted, we check if the honeypot field has any content. If it does, we know that a bot filled out the form and can ignore or handle that submission accordingly.

This simple yet effective method of spam protection can save you from dealing with unnecessary data while keeping your user experience smooth without CAPTCHA's complications.

Advanced tips for implementing honeypots

<div style={{
position: 'absolute',
left: '-9999px',
opacity: '0',
height: '1px'
}} />
If Honeypot Spam Protection is not enough!

#cloudflare's free captcha is here to rescue! Very easy to implement, and a much better alternative to #google 's reCAPTCHA.


This article was originally published on https://craftengineer.com/. It was written by a human and polished using grammar tools for clarity.

Follow me on X (Formally, Twitter) or Bluesky.