How does better-auth plugin generate customer IDs?

dev
1764ce3Merge branch 'main' into dev
This post might have stale content, as dev is 893 commits ahead.

How does the better-auth plugin work? The customer IDs are generated by Autumn or as passed by better-auth?

Avatar of anthropic/claude-haiku-4.5
Claude Haiku 4.5
commented

Perfect! Now I have enough information. Let me provide a comprehensive answer based on my findings.

##Summary

Based on my exploration of the Autumn codebase and better-auth documentation, here's how the customer IDs work:

###Customer ID Generation Pattern

Customer IDs are generated by Autumn, not by better-auth:

  1. Internal ID Generation: When creating a customer in Autumn, an internal ID is generated using Autumn's own ID generation system:

    // From server/src/utils/genUtils.ts
    export const generateId = (prefix: string) => {
        if (!prefix) {
            return KSUID.randomSync().string;
        } else {
            return `${prefix}_${KSUID.randomSync().string}`;
        }
    };
    

    Autumn uses KSUID (Keyed Sortable Unique ID) with a cus_ prefix for customers:

    // From server/src/internal/customers/cusUtils/createNewCustomer.ts
    const internalId = generateId("cus");  // generates something like "cus_<ksuid>"
    
  2. Optional External ID: When creating a customer via the API, you can optionally pass your own id:

    // From server/src/internal/customers/handlers/handlePostCustomerV2.ts
    const createCusParams = c.req.valid("json");
    const baseData = await getOrCreateApiCustomer({
        ctx,
        customerId: createCusParams.id,  // optional customer ID passed by caller
        customerData: createCusParams,
    });
    

###How it Works with better-auth

In the Autumn + better-auth integration, the typical flow is:

  1. Authentication: User authenticates through better-auth

  2. User ID Resolution: Autumn gets the user ID from the better-auth session:

    // From vite/src/views/onboarding/onboarding-steps/MountHandler.tsx
    const session = await auth.api.getSession({
        headers: request.headers,
    });
    
    return {
        customerId: session?.user.id,  // Uses better-auth user ID as customer ID
        customerData: {
            name: session?.user.name,
            email: session?.user.email,
        },
    };
    
  3. Customer Creation: Autumn receives this ID and creates/retrieves the customer

###Stripe Integration Note

While @better-auth/stripe is in the dependencies, the actual implementation in this codebase primarily uses:

  • better-auth for authentication (organizations, email/password, etc.)
  • Autumn for billing/subscription management (which then connects to Stripe)
  • The @better-auth/stripe plugin is NOT actively configured in server/src/utils/auth.ts

The codebase shows Autumn has its own Stripe integration layer (/server/src/external/stripe/) rather than relying on better-auth's Stripe plugin. Customer IDs in Autumn are internal Autumn identifiers that can optionally map to external identifiers (like Stripe customer IDs), but these are separate from the user IDs that come from better-auth.


END OF POST

How does better-auth plugin generate customer IDs? — useautumn/autumn