Install using bunx shadcn@latest add @remix-utils/honeypot-server and bunx shadcn@latest add @remix-utils/honeypot-react.
Note
This depends on react and @oslojs/crypto, and @oslojs/encoding.
Honeypot is a simple technique to prevent spam bots from submitting forms. It works by adding a hidden field to the form that bots will fill, but humans won't.
There's a pair of utils in Remix Utils to help you implement this.
First, create a honeypot.server.ts where you will instantiate and configure your Honeypot.
// Create a new Honeypot instance, the values here are the defaults, you can // customize them exportconsthoneypot = newHoneypot({ randomizeNameFieldName:false, nameFieldName:"name__confirm", validFromFieldName:"from__confirm", // null to disable it encryptionSeed:undefined, // Ideally it should be unique even between processes });
Then, in your app/root loader, call honeypot.getInputProps() and return it.
exportdefaultfunctionComponent() { // more code here return ( // some JSX <HoneypotProvider{...honeypotInputProps}> <Outlet/> </HoneypotProvider> // end that JSX ); }
Now, in every public form you want protect against spam (like a login form), render the HoneypotInputs component.
functionSomePublicForm() { return ( <Formmethod="post"> <HoneypotInputslabel="Please leave this field blank"/> {/* more inputs and some buttons * /} </Form> ); }
Note
The label value above is the default one, use it to allow the label to be localized, or remove it if you don't want to change it.
Finally, in the action the form submits to, you can call honeypot.check.
exportasyncfunctionaction({ request }) { letformData = awaitrequest.formData(); try { honeypot.check(formData); } catch (error) { if (errorinstanceofSpamError) { // handle spam requests here } // handle any other possible error here, e.g. re-throw since nothing else // should be thrown } // the rest of your action }
Install using
bunx shadcn@latest add @remix-utils/honeypot-serverandbunx shadcn@latest add @remix-utils/honeypot-react.This depends on
reactand@oslojs/crypto, and@oslojs/encoding.Honeypot is a simple technique to prevent spam bots from submitting forms. It works by adding a hidden field to the form that bots will fill, but humans won't.
There's a pair of utils in Remix Utils to help you implement this.
First, create a
honeypot.server.tswhere you will instantiate and configure your Honeypot.Then, in your
app/rootloader, callhoneypot.getInputProps()and return it.And in the
app/rootcomponent render theHoneypotProvidercomponent wrapping the rest of the UI.Now, in every public form you want protect against spam (like a login form), render the
HoneypotInputscomponent.The label value above is the default one, use it to allow the label to be localized, or remove it if you don't want to change it.
Finally, in the action the form submits to, you can call
honeypot.check.Author
Sergio XalambrÃ