Lumen
Integrations

better-auth

Integrate Lumen with better-auth

Automatically assign your free plan to new users

In order for entitlements to work, all your users should have a plan assigned. We recommend automatically assigning your free plan to every new user in your auth system.

Requirements

You must have an active free plan. If you don't, create one at Plans.

Installation

npm install @getlumen/better-auth

Usage

Basic Usage

import { betterAuth } from "better-auth";
import { LumenPlugin } from "@getlumen/better-auth";

export const auth = betterAuth({
  // ... your other Better Auth configuration

  plugins: [
    LumenPlugin({
      apiKey: process.env.LUMEN_API_KEY!,
    }),
  ],
});

Advanced Usage with Callbacks

import { betterAuth } from "better-auth";
import { LumenPlugin } from "@getlumen/better-auth";

export const auth = betterAuth({
  // ... your other Better Auth configuration

  plugins: [
    LumenPlugin({
      apiUrl: process.env.API_URL, // you can put http://localhost:backend_port for development
      apiKey: process.env.LUMEN_API_KEY!,
      enabled: true,
      prodOnly: process.env.NODE_ENV !== "development",
      onSuccess: (user, subscription) => {
        console.log(
          `Free subscription created for ${user.email}:`,
          subscription
        );
        // Optional: Send notification, update analytics, etc.
      },
      onError: (user, error) => {
        console.error(
          `Failed to create free subscription for ${user.email}:`,
          error
        );
        // Optional: Send error notification, log to monitoring service, etc.
      },
      logger: (message, ...args) => {
        console.log(`[FreeSubscription] ${message}`, ...args);
      },
    }),
  ],
});

Integration with Other Plugins

If you have other plugins, you can combine them:

import { betterAuth } from "better-auth";
import { LumenPlugin } from "@getlumen/better-auth";
import { someOtherPlugin } from "some-other-plugin";

export const auth = betterAuth({
  // ... your other Better Auth configuration

  plugins: [
    someOtherPlugin(),
    LumenPlugin({
      apiUrl: process.env.API_URL || "https://api.getlumen.dev",
      apiKey: process.env.LUMEN_API_KEY!,
      enabled: true,
      prodOnly: true,
    }),
  ],
});

Configuration Options

OptionTypeDefaultDescription
apiUrlstringhttps://api.getlumen.devThe base URL of your API service, you can put http://localhost:backend_port for development
apiKeystringRequiredAPI key for authenticating with the subscription service
enabledbooleantrueWhether the plugin is enabled
prodOnlybooleanfalseWhether to only run in production/staging environments
onSuccessfunctionOptionalCallback function called when subscription is created successfully
onErrorfunctionOptionalCallback function called when subscription creation fails
loggerfunctionconsole.logCustom logger function for debugging

How It Works

  1. The plugin hooks into the Better Auth after hook with a matcher
  2. It checks if the current request is a sign-in endpoint (email login or social callback)
  3. If a new session is created, it automatically calls the /v1/subscriptions/create-free-subscription endpoint
  4. The subscription is created with the user's email, name, and ID
  5. Success/error callbacks are triggered accordingly

Error Handling

The plugin includes comprehensive error handling:

  • Missing API key will skip subscription creation
  • Network errors are caught and logged
  • API errors are parsed and passed to the error callback
  • Errors don't block user authentication (fail gracefully)

Development vs Production

When prodOnly is set to true, the plugin will skip execution in development mode. This is useful for preventing test subscriptions from being created during development.