Skip to content

Signup form protection for Next.js

Arcjet signup form protection combines rate limiting, bot protection, and email validation to protect your signup forms from abuse.

What is Arcjet? Arcjet helps developers protect their apps in just a few lines of code. Implement rate limiting, bot protection, email verification & defend against common attacks.

Quick start

This guide will show you how to protect a Next.js signup form.

1. Install SDK

In your project root, run the following command to install the SDK:

Terminal window
npm i @arcjet/next

2. Set your key

Create a free Arcjet account then follow the instructions to add a site and get a key. Add it to a .env.local file in your Next.js project root:

.env.local
ARCJET_KEY=ajkey_yourkey

3. Protect a form

Arcjet signup form protection is a combination of the rate limiting, bot protection, and email validation primitives. These are configured using our recommended rules.

The example below is a simple email form that submits to an API route. You could adapt this as part of a signup form.

Create the API route handler to receive the form submission:

/app/api/submit/route.ts
import arcjet, { protectSignup } from "@arcjet/next";
import { NextResponse } from "next/server";
const aj = arcjet({
key: process.env.ARCJET_KEY!, // Get your site key from https://app.arcjet.com
rules: [
protectSignup({
email: {
mode: "LIVE", // will block requests. Use "DRY_RUN" to log only
// Block emails that are disposable, invalid, or have no MX records
block: ["DISPOSABLE", "INVALID", "NO_MX_RECORDS"],
},
bots: {
mode: "LIVE",
// Block clients that we are sure are automated
block: ["AUTOMATED"],
},
// It would be unusual for a form to be submitted more than 5 times in 10
// minutes from the same IP address
rateLimit: {
// uses a sliding window rate limit
mode: "LIVE",
interval: "10m", // counts requests over a 10 minute sliding window
max: 5, // allows 5 submissions within the window
},
}),
],
});
export async function POST(req: Request) {
const data = await req.json();
const email = data.email;
const decision = await aj.protect(req, {
email,
});
console.log("Arcjet decision: ", decision);
if (decision.isDenied()) {
if (decision.reason.isEmail()) {
return NextResponse.json(
{
message: "Invalid email",
reason: decision.reason,
},
{ status: 400 },
);
} else {
return NextResponse.json({ message: "Forbidden" }, { status: 403 });
}
} else {
return NextResponse.json({
message: "Hello world",
});
}
}

Next, create the form page:

/app/form/page.tsx
"use client";
import React, { useState, type FormEvent } from "react";
export default function Page() {
const [isLoading, setIsLoading] = useState<boolean>(false);
const [error, setError] = useState<string | null>(null);
async function onSubmit(event: FormEvent<HTMLFormElement>) {
event.preventDefault();
setIsLoading(true);
setError(null); // Clear previous errors when a new request starts
try {
const formData = new FormData(event.currentTarget);
const response = await fetch("/api_app/form", {
method: "POST",
body: JSON.stringify(Object.fromEntries(formData)),
headers: {
"Content-Type": "application/json",
},
});
if (!response.ok) {
const error = await response.json();
throw new Error(
`${response.status} ${response.statusText}: ${error.message}`,
);
}
// Handle response if necessary
const data = await response.json();
// ...
} catch (error: any) {
// Capture the error message to display to the user
setError(error.message);
console.error(error);
} finally {
setIsLoading(false);
}
}
return (
<div>
{error && <div style={{ color: "red" }}>{error}</div>}
<form onSubmit={onSubmit}>
<label htmlFor="email">Email</label>
{/*
This is a "text" input type rather than "email" to demonstrate
Arcjet validating invalid emails. Changing to "email" will allow
the browser to validate as well
*/}
<input
type="text"
defaultValue={"invalid@email"}
name="email"
id="email"
/>
<button type="submit" disabled={isLoading}>
{isLoading ? "Loading..." : "Submit"}
</button>
</form>
</div>
);
}

4. Start app

Start your app and load http://localhost:3000/form. Submit the form with a variety of email addresses and you can see how the check behaves. The requests will also show up in the Arcjet dashboard.

FAQs

Do I need to run any infrastructure e.g. Redis?

No, Arcjet handles all the infrastructure for you so you don't need to worry about deploying global Redis clusters, designing data structures to track rate limits, or keeping security detection rules up to date.

What is the performance overhead?

Arcjet SDK tries to do as much as possible asynchronously and locally to minimize latency for each request. Where decisions can be made locally or previous decisions are cached in-memory, latency is usually <1ms.

When a call to the Arcjet API is required, such as when tracking a rate limit in a serverless environment, there is some additional latency before a decision is made. The Arcjet API has been designed for high performance and low latency, and is deployed to multiple regions around the world. The SDK will automatically use the closest region which means the total overhead is typically no more than 20-30ms, often significantly less.

What happens if Arcjet is unavailable?

Where a decision has been cached locally e.g. blocking a client, Arcjet will continue to function even if the service is unavailable.

If a call to the Arcjet API is needed and there is a network problem or Arcjet is unavailable, the default behavior is to fail open and allow the request. You have control over how to handle errors, including choosing to fail close if you prefer. See the reference docs for details.

How does Arcjet protect me against DDoS attacks?

Network layer attacks tend to be generic and high volume, so these are best handled by your hosting platform. Most cloud providers include network DDoS protection by default.

Arcjet sits closer to your application so it can understand the context. This is important because some types of traffic may not look like a DDoS attack, but can still have the same effect. For example, a customer making too many API requests and affecting other customers, or large numbers of signups from disposable email addresses.

Network-level DDoS protection tools find it difficult to protect against this type of traffic because they don't understand the structure of your application. Arcjet can help you to identify and block this traffic by integrating with your codebase and understanding the context of the request e.g. the customer ID or sensitivity of the API route.

Volumetric network attacks are best handled by your hosting provider. Application level attacks need to be handled by the application. That's where Arcjet helps.

What next?

Get help

Need help with anything? Email us or join our Discord to get support from our engineering team.