Better-Auth Disposable Email Blocker

0.2.0 · active · verified Wed Apr 22

The `better-auth-no-disposable-emails` package is a plugin for the `better-auth` authentication library, designed to prevent users from signing up or signing in with temporary or disposable email addresses. It integrates with `better-auth`'s plugin system, intercepting specified authentication endpoints (e.g., `/sign-up/email`) and validating the submitted email against a comprehensive list of known disposable email providers, powered by the `mailchecker` library. The current stable version is 0.2.0, indicating it's relatively new and in active development, though a specific release cadence isn't published. Its key differentiator is its seamless integration with `better-auth`, providing a configurable and extensible solution to enhance user quality and reduce spam registrations by blocking transient email accounts.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `better-auth` and integrate the `noDisposableEmails` plugin, configuring it with a custom error message, additional blocked domains, and specific interception paths.

import { betterAuth } from "better-auth";
import { noDisposableEmails } from "better-auth-no-disposable-emails";

// Mock database adapter for demonstration purposes
const mockDatabaseAdapter = {
  getUserByEmail: async (email: string) => {
    if (email === 'test@example.com') return { id: '1', email: 'test@example.com' };
    return null;
  },
  createUser: async (email: string, passwordHash: string) => ({
    id: String(Date.now()),
    email,
  }),
  updateUser: async (id: string, updates: any) => ({ id, ...updates }),
  // Add other necessary methods for a better-auth database adapter
};

export const auth = betterAuth({
  database: mockDatabaseAdapter, // Replace with your actual database adapter
  emailAndPassword: {
    enabled: true,
    // In a real app, you'd configure password hashing, JWTs, etc.
    getHash: async (password: string) => password + '_hashed', // Placeholder
    verifyHash: async (password: string, hash: string) => (password + '_hashed') === hash // Placeholder
  },
  plugins: [
    noDisposableEmails({
      errorMessage: "Please use a permanent email address for registration.",
      customBlockedDomains: ["temporary.io", "spammail.net"],
      paths: ["/sign-up/email", "/sign-in/magic-link"]
    }),
  ],
  // ... other better-auth configuration
});

console.log("better-auth instance with disposable email plugin configured.");
console.log("Plugin paths: /sign-up/email, /sign-in/magic-link");

view raw JSON →