From 10893badbcf2c9ac515548f168c8def267bcceca Mon Sep 17 00:00:00 2001 From: Maxwell <145994855+ping-maxwell@users.noreply.github.com> Date: Fri, 4 Apr 2025 04:23:43 +1000 Subject: [PATCH] fix(stripe): allow customizing subscription schema (#2105) This was documented in docs at https://www.better-auth.com/docs/plugins/stripe#customizing-the-schema but wasn't implemented. This is now fixed. --- packages/stripe/src/schema.ts | 125 +++++++++++++++++++--------------- packages/stripe/src/types.ts | 7 +- 2 files changed, 75 insertions(+), 57 deletions(-) diff --git a/packages/stripe/src/schema.ts b/packages/stripe/src/schema.ts index d4f8fd7917..07a0e8df10 100644 --- a/packages/stripe/src/schema.ts +++ b/packages/stripe/src/schema.ts @@ -1,62 +1,75 @@ import type { AuthPluginSchema } from "better-auth"; import type { StripeOptions } from "./types"; +import { mergeSchema } from "better-auth/db"; + +export const subscriptions = { + subscription: { + fields: { + plan: { + type: "string", + required: true, + }, + referenceId: { + type: "string", + required: true, + }, + stripeCustomerId: { + type: "string", + required: false, + }, + stripeSubscriptionId: { + type: "string", + required: false, + }, + status: { + type: "string", + defaultValue: "incomplete", + }, + periodStart: { + type: "date", + required: false, + }, + periodEnd: { + type: "date", + required: false, + }, + cancelAtPeriodEnd: { + type: "boolean", + required: false, + defaultValue: false, + }, + seats: { + type: "number", + required: false, + }, + }, + }, +} satisfies AuthPluginSchema; + +export const user = { + user: { + fields: { + stripeCustomerId: { + type: "string", + required: false, + }, + }, + }, +} satisfies AuthPluginSchema; export const getSchema = (options: StripeOptions) => { - const subscriptions = { - subscription: { - fields: { - plan: { - type: "string", - required: true, - }, - referenceId: { - type: "string", - required: true, - }, - stripeCustomerId: { - type: "string", - required: false, - }, - stripeSubscriptionId: { - type: "string", - required: false, - }, - status: { - type: "string", - defaultValue: "incomplete", - }, - periodStart: { - type: "date", - required: false, - }, - periodEnd: { - type: "date", - required: false, - }, - cancelAtPeriodEnd: { - type: "boolean", - required: false, - defaultValue: false, - }, - seats: { - type: "number", - required: false, - }, - }, + if ( + options.schema && + !options.subscription?.enabled && + "subscription" in options.schema + ) { + options.schema.subscription = undefined; + } + return mergeSchema( + { + ...(options.subscription?.enabled ? subscriptions : {}), + ...user, }, - } satisfies AuthPluginSchema; - const user = { - user: { - fields: { - stripeCustomerId: { - type: "string", - required: false, - }, - }, - }, - } satisfies AuthPluginSchema; - return { - ...(options.subscription?.enabled ? subscriptions : {}), - ...user, - } as typeof user & typeof subscriptions; + options.schema, + ); }; diff --git a/packages/stripe/src/types.ts b/packages/stripe/src/types.ts index 0644782dec..1af2f30c4a 100644 --- a/packages/stripe/src/types.ts +++ b/packages/stripe/src/types.ts @@ -1,5 +1,6 @@ -import type { Session, User } from "better-auth"; +import type { InferOptionSchema, Session, User } from "better-auth"; import type Stripe from "stripe"; +import type { subscriptions, user } from "./schema"; export type StripePlan = { /** @@ -312,6 +313,10 @@ export interface StripeOptions { }; }; onEvent?: (event: Stripe.Event) => Promise; + /** + * Schema for the stripe plugin + */ + schema?: InferOptionSchema; } export interface Customer {