Integrations · Sign-up and auth

Stop bad emails at Better Auth sign-up

Keep sign-ups with bad email addresses out of an app that uses Better Auth. A small server plugin checks each new user's address with the API. When the result is a status you chose to stop (by default invalid and disposable), Better Auth refuses the sign-up. Returning users are never checked. If the API is slow or down, the sign-up goes through.

It is one TypeScript file under the MIT licence (the text is at the top of the file). It imports only from better-auth, which your app already has, and is built on Better Auth 1.7's plugin API. Download it: email-validation-better-auth.ts.

How it works

Better Auth runs a database hook, user.create.before, before it creates any new user. The plugin adds that hook and refuses the creation when the address is stopped. Magic link and email OTP sign-ups are checked earlier, before the link or code is sent, so no mail goes to a bad address.

Sign-in methodWhen a stopped user hears about it
Email and passwordRight away: signUp.email() returns an error with code EMAIL_ADDRESS_NOT_ACCEPTED
Magic linkRight away: signIn.magicLink() returns the error and no link is sent
Email OTPRight away: emailOtp.sendVerificationOtp() returns the error and no code is sent
Google, GitHub and other OAuth providersAfter the provider's consent screen: Better Auth redirects to your errorCallbackURL with ?error=EMAIL_ADDRESS_NOT_ACCEPTED

A returning user creates no new user, so is never checked. That includes someone who links a new OAuth provider to an account they already have. Anonymous, phone number and Sign-In with Ethereum sign-ups are skipped by default, because their addresses are usually made up by Better Auth; you can choose to check them. A user who changes their address later is not checked.

Set it up

  1. Create an API key on the API keys page with the permission Validate addresses and a daily credit cap. Put it in your server's environment as EV_API_KEY, never in browser code.

  2. Copy email-validation-better-auth.ts next to your Better Auth config.

  3. Add the plugin to your server config. Do not add it to createAuthClient():

    import { betterAuth } from 'better-auth';
    import { emailValidation } from './email-validation-better-auth';
    
    export const auth = betterAuth({
      // ...your database, emailAndPassword, socialProviders, plugins...
      plugins: [emailValidation({ apiKey: process.env.EV_API_KEY })],
    });

    It needs no database tables and no migration.

  4. Show the message when a sign-up is stopped:

    const { error } = await authClient.signUp.email({ email, password, name });
    if (error?.code === 'EMAIL_ADDRESS_NOT_ACCEPTED') setMessage(error.message);

    For OAuth, read error from the query string of the page you passed as errorCallbackURL.

  5. Test it with an email and password sign-up and a test address, such as disposable@sandbox.email-validation.org: it is refused with EMAIL_ADDRESS_NOT_ACCEPTED. Test addresses are free and always give the same answer. They receive no mail, so do not use them for magic link or OTP sign-ups that would really send one, nor sign up with one that passes: it creates the user, and a verification or welcome mail to it would bounce.

Options

OptionDefaultWhat it does
apiKeyrequiredYour API key. Without one, nothing is checked.
regionuseu validates addresses in the EU and stores their results there (EU data residency)
stop['invalid', 'disposable']The statuses that stop a sign-up: any of invalid, disposable, catch_all, unknown
timeoutMs6000How long to wait for an answer before the sign-up goes through: whole milliseconds from 1 to 60000; any other value throws a TypeError when the plugin is created
messagePlease sign up with another email address.The message a stopped sign-up gets
checkBeforeSendingtrueCheck magic link and email OTP sign-ups before the link or code is sent
skipanonymous, phone number, SIWEA function that returns true to let a sign-up through unchecked
onResultnoneCalled after each check with the method 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 our API suggests a correction for a typo, the error body of an email and password sign-up carries it as didYouMean.

Returning users are never checked, so on the magic link and email OTP endpoints a caller can tell an address with an account from one without. Set checkBeforeSending: false to check when the link or code is used instead, and keep Better Auth's rate limiting on.

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. A check is never retried.

What it costs

Each check costs at most 1 credit, like any validation request. unknown results, errors, malformed addresses and test addresses are free. A magic link or code that was checked when it was sent is not checked again when it is used on the same server; on serverless hosts it may be, so one sign-up can cost up to 2 credits. 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). Keep Better Auth's rate limiting on and give the key a daily credit cap.

Privacy

The plugin 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, and there is no client plugin, so the key never reaches the browser. Nothing is logged, and onResult never gets the address or the suggested correction.

More on Better Auth

Updated · Maintained by: Email Validation LLC

Need help? Contact support, or ask the help chat. Something wrong or missing on this page? Tell us