Better Auth · TypeScript

Block disposable email in Better Auth

Better Auth fires a databaseHooks.user.create.before hook for every signup path — email/password, Google OAuth, magic link. One check covers them all.

The code

// lib/auth.ts
import { betterAuth } from "better-auth";

const CHECK_URL = "https://api.piisend.com/api/v1/check/email";

async function shouldBlock(email: string) {
  const r = await fetch(`${CHECK_URL}/${encodeURIComponent(email)}`, {
    headers: {
      Authorization: `Bearer ${process.env.PIISEND_API_KEY!}`,
    },
  });
  if (!r.ok) return false;
  // reject on 'block' — catches disposable, spam, no-MX and typo'd addresses
  return (await r.json()).recommendation === 'block';
}

export const auth = betterAuth({
  databaseHooks: {
    user: {
      create: {
        before: async (user) => {
          if (user.email && await shouldBlock(user.email)) {
            throw new Error("Please use a permanent email address.");
          }
          return { data: user };
        },
      },
    },
  },
});

API key

Keep `PIISEND_API_KEY` in server env only — Better Auth hooks run server-side.

Fail-open vs fail-closed

Wrap `isDisposable` in try/catch and return false on error if you prefer availability over strict blocking.

Pair with verification

The thrown error surfaces to your signup UI via Better Auth's normal error handling.

FAQ

Where does the Piisend API key go in Better Auth?

Keep `PIISEND_API_KEY` in server env only — Better Auth hooks run server-side.

What happens if Piisend is slow or down?

Wrap `isDisposable` in try/catch and return false on error if you prefer availability over strict blocking.

How does this fit with the rest of Better Auth signup?

The thrown error surfaces to your signup UI via Better Auth's normal error handling.

What does this snippet block?

Addresses on disposable domains: about 10,900 listed domains, plus new ones whose mail servers belong to a known throwaway service. Free providers such as Gmail and forwarding aliases such as duck.com pass.

Can I get a full allow / review / block verdict instead?

Yes, and we recommend it. Call GET /api/v1/check/email/{email} instead. Both endpoints need an API key with the check:read scope and use one check from your monthly allowance per call, but the check endpoint also returns forwarding relays, phishing lists, missing MX records, new domains, typo suggestions and an allow / review / block verdict.

Send verification email with Piisend

After blocking disposable addresses, send OTP and verification mail through the same Piisend account. One API key for intelligence checks and transactional sends.