Better Auth Lead Plugin

0.4.0 · active · verified Wed Apr 22

The `better-auth-lead` package is a plugin for the `better-auth` ecosystem, designed to extend its core functionality with lead management capabilities. It facilitates features like newsletter sign-ups, waitlists, and general lead capture by providing a dedicated database table (`leads`) and a robust API. Currently at version 0.4.0, the package is in active development, implying potential API changes in minor versions before reaching a stable 1.0 release. Key differentiators include its seamless integration with `better-auth`'s server and client infrastructure, built-in support for email verification with customizable sending logic, and the ability to define and validate lead metadata using standard schema libraries like Zod or Valibot. This allows developers to quickly add lead generation features with strong type safety and validation.

Common errors

Warnings

Install

Imports

Quickstart

This code snippet demonstrates how to configure the `better-auth-lead` server plugin within `createBetterAuth`, including custom logic for sending verification emails and handling post-verification actions. It includes basic rate-limiting for email sending.

import { createBetterAuth } from '@better-auth/core';
import { lead } from 'better-auth-lead';
// Assume 'sendEmail' is a function that sends an email
// e.g., using nodemailer, resend, or a custom service.
async function sendEmail({ to, subject, text }: { to: string; subject: string; text: string }) {
  console.log(`Sending email to ${to} with subject: ${subject}`);
  console.log(`Body: ${text}`);
  // In a real application, integrate with an email sending service here.
  // Example: await resend.emails.send({ to, subject, text });
}

const betterAuth = createBetterAuth({
  plugins: [
    lead({
      sendVerificationEmail: async ({ lead, url }) => {
        const { verificationEmailSentAt } = lead;
        // Implement basic rate-limiting to prevent sending too many emails
        if (
          verificationEmailSentAt &&
          Date.now() - verificationEmailSentAt.getTime() < 60 * 1000 // 1 minute cooldown
        ) {
          console.log(
            `Skipping verification email for ${lead.email}: recent email already sent.`,
          );
          return false;
        }

        void sendEmail({
          to: lead.email,
          subject: 'Please verify your email address',
          text: `Click the link to verify your email: ${url}`,
        });

        return true; // Indicate that an email was sent
      },
      onEmailVerified: async ({ lead }) => {
        console.log(`Lead ${lead.email} has been successfully verified!`);
        // Additional logic after email verification, e.g., update CRM, send welcome email
      }
    }),
  ],
});

// To use betterAuth, you would typically export it or integrate it into an HTTP server setup.
// For example, in a Next.js API route or an Express app.
// console.log('Better Auth with Lead plugin configured.');

view raw JSON →