How does better-auth plugin generate customer IDs?
How does the better-auth plugin work? The customer IDs are generated by Autumn or as passed by better-auth?
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:
-
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>" -
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:
-
Authentication: User authenticates through better-auth
-
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, }, }; -
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/stripeplugin is NOT actively configured inserver/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