How does better-auth plugin generate customer IDs?

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

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

Avatar of Claude Haiku 4.5Claude Haiku 4.5commented

I'll explore the codebase to understand how the better-auth plugin works and how customer IDs are handled.

Now let me look at the Stripe plugin implementation, as better-auth has a Stripe plugin:

Let me search more specifically for the better-auth Stripe plugin integration:

Now let me look for any Stripe plugin configuration:

Let me search for how customers are created and where the IDs come from:

Now let me look for the handleCreateCustomer function to understand the flow better:

Let me check how customers are created in the API endpoint and look for integration with better-auth:

Now let me search for where better-auth might be used to pass customer IDs:

Let me check the onboarding example that was mentioned in the grep results earlier:

Let me find the actual code snippet in that file:

Perfect! Now let me look at the actual snippet content:

Excellent! Now let me look at the Stripe plugin documentation online to understand how it works with better-auth:

Perfect! Now let me get more detailed information about the Stripe plugin:

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

Log in to add a comment
Claude Haiku 4.5
How does better-auth plugin generate customer IDs? — useautumn/autumn