Integrations · Sign-up and auth
Stop bad emails at Auth.js sign-up
Keep sign-ups with bad email addresses out of an app that uses Auth.js (NextAuth.js v5). A small file you add to your Auth.js configuration checks each new user's address with the API. When the result is a status you chose to stop (by default invalid and disposable), Auth.js refuses the sign-up. If the API is slow or down, the sign-up goes through.
It is one TypeScript file with no dependencies, under the MIT licence (the text is at the top of the file). Download it: email-validation-authjs.ts. It runs on the server side of your app, next to your Auth.js configuration, and never in a browser.
How it works, and its limits
Auth.js lets an app refuse a sign-in in one place: the signIn callback. The file gives you one to use there. With your adapter or an isNewUser function it checks only new users, so a returning user is never locked out; without them, checkEverySignIn: true (below) checks every sign-in. (A signed-in user who links another provider with a different address is checked; give your own isNewUser to leave that out.)
- Social sign-in (OAuth, OIDC): checked after the provider answers, before Auth.js creates the user. A stopped address gets Auth.js's "Access denied" page and no account. A provider that shares no address is let through.
- Magic links: checked before the mail is sent. A stopped address gets no mail and no account. When the link is opened, the address is not checked again.
- Passkeys (WebAuthn, experimental in Auth.js): checked before a new passkey account is created. A stopped passkey sign-up always gets Auth.js's error page.
- Email and password (Credentials): Auth.js has no sign-up for these: your own code creates the users. Call the file's
checkSignupEmail()there, before you create the user.
To tell new users from returning ones, the file asks your Auth.js database adapter whether the person already has an account. Without an adapter (sessions in a JWT only), Auth.js keeps no users and cannot tell. Then you choose: give your own isNewUser function, or set checkEverySignIn: true and check every sign-in, returning users too.
Set it up
Create an API key on the API keys page with the permission Validate addresses and a daily credit cap. Put it in the server variable
EV_API_KEY, never in aNEXT_PUBLIC_variable or browser code.Copy
email-validation-authjs.tsinto your app, for example tolib/.Add it to
auth.ts:import NextAuth from 'next-auth'; import { createSignInCheck } from '@/lib/email-validation-authjs'; const adapter = PrismaAdapter(prisma); // the adapter you already use const checkEmail = createSignInCheck({ apiKey: process.env.EV_API_KEY, adapter }); export const { handlers, auth, signIn, signOut } = NextAuth({ adapter, providers: [/* … */], callbacks: { signIn: checkEmail }, });If you have a
signIncallback already, callcheckEmail(params)first and return its answer when it is nottrue. SvelteKit, Express and Qwik take the same configuration; only the import ofNextAuthchanges. With Express,@auth/expresscurrently needs Express 4: it does not mount on Express 5.Test it on the server:
checkSignupEmail('disposable@sandbox.email-validation.org', { apiKey })answersallow: false. It is a test address: free, and it always answersdisposable. Then a magic link asked for that address shows Auth.js's error page and sends no mail. Test addresses receive no mail, so ask for a magic link only for a test address the check stops.
For email and password sign-ups, check in your own registration code, before you create the user:
'use server';
import { checkSignupEmail } from '@/lib/email-validation-authjs';
export async function register(email: string, password: string) {
const d = await checkSignupEmail(email, { apiKey: process.env.EV_API_KEY });
if (!d.allow) return { error: d.didYouMean ? `Did you mean ${d.didYouMean}?` : 'Please use another email address.' };
// …create the user
}Without an adapter, pass your own record of users, or check every sign-in:
createSignInCheck({ apiKey: process.env.EV_API_KEY, isNewUser: async ({ user }) => !(await db.userExists(user.email)) });
createSignInCheck({ apiKey: process.env.EV_API_KEY, checkEverySignIn: true });Choose what to stop
| Option | Default | What it does |
|---|---|---|
stop | ['invalid', 'disposable'] | Statuses that stop a sign-up: any of invalid, disposable, catch_all, unknown |
region | us | eu validates addresses in the EU and stores their results there (EU data residency) |
timeoutMs | 6000 | How long to wait for an answer before the sign-up goes through: whole milliseconds from 1 to 60000; any other value throws a TypeError |
redirectTo | Auth.js's error page | Where a stopped sign-up goes, for example /signup?error=email (not for passkeys) |
onResult | none | Called once per sign-in with the provider, the outcome and the result, never the address |
A result marked disposable is stopped while disposable is in stop, whatever its status (the API keeps invalid or unknown as the status of some addresses on disposable domains), except an unknown that means our check ran out of time or failed (timeout, internal_error). Otherwise a valid result goes through, and an unknown result is stopped only if you add it to stop, never for timeout or internal_error.
When something fails
No key, a timeout, a network error, a rate limit, a refused key, no credits or any other error answer lets the sign-up through, and so does a failing database lookup. The file waits at most 6 seconds for an answer and never retries. It never throws into Auth.js.
What it costs
Each check costs at most 1 credit, like any validation request. unknown results, errors, malformed addresses and test addresses are free. With your adapter or isNewUser, returning users are not checked; with checkEverySignIn: true instead, every sign-in is checked and costs a check. Credits come in one-time packs, from $0.15 per 1,000 with the 50,000,000-credit pack (smaller packs cost more per credit), and new accounts get 500 free credits (100 when you sign up with a free email provider such as Gmail, Outlook or Yahoo, another personal mailbox such as a paid one like Fastmail or one from your internet provider, or an address from a relay or alias service). Anyone can start a sign-in with any address, so give the key a daily credit cap and rate-limit your sign-in route.
Privacy
The file sends only the new user's address, in the body of a POST to https://api.email-validation.org (or https://api-eu.email-validation.org). It never puts the address in a URL and sends it to no other host. Your API key goes only to the API. The file never logs an address, and what it passes to onResult holds the provider and the result, not the address. A refusal reaches Auth.js's logger only as AccessDenied.