Skip to content

feat: integrate Stripe for subscription management and update schema #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ jobs:
cache: "npm"
- run: npm i
- run: npm ci
- run: cd example && npm i && cd ..
# - run: cd example && npm i && cd ..
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this intentional?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, because the CI completely fails, since there is no "example" directory. I guess in there there should be the generate command maybe?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, we have a bunch of examples. Need to fix this.

- run: npm test
10 changes: 10 additions & 0 deletions auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
username,
} from "better-auth/plugins";
import { convex } from "@convex-dev/better-auth/plugins";
import { stripe } from "@better-auth/stripe";

// This is the config used to generate the schema
const config = betterAuth({
Expand All @@ -19,6 +20,14 @@ const config = betterAuth({
emailOTP({ sendVerificationOTP: async () => {} }),
anonymous(),
username(),
stripe({
stripeClient: {} as any,
stripeWebhookSecret: '',
subscription: {
enabled: true,
plans: []
}
}),
convex(),
],
});
Expand All @@ -28,6 +37,7 @@ export { config as auth };
// all fields in the schema specialFields are automatically indexed
export const indexFields = {
user: ["userId", "name", ["email", "name"]],
subscription: ["stripeCustomerId", "stripeSubscriptionId", "referenceId"],
session: ["expiresAt", ["expiresAt", "userId"]],
account: ["accountId", ["accountId", "providerId"], ["providerId", "userId"]],
verification: ["expiresAt", "identifier"],
Expand Down
75 changes: 75 additions & 0 deletions examples/next/convex/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export declare const components: {
image?: string;
isAnonymous?: boolean;
name: string;
stripeCustomerId?: string;
twoFactorEnabled?: boolean;
updatedAt: number;
userId?: string;
Expand Down Expand Up @@ -244,6 +245,42 @@ export declare const components: {
| null;
}>;
}
| {
data: {
cancelAtPeriodEnd?: boolean;
periodEnd?: number;
periodStart?: number;
plan: string;
referenceId: string;
seats?: number;
status?: string;
stripeCustomerId?: string;
stripeSubscriptionId?: string;
};
model: "subscription";
where?: Array<{
connector?: "AND" | "OR";
field: string;
operator?:
| "lt"
| "lte"
| "gt"
| "gte"
| "eq"
| "in"
| "ne"
| "contains"
| "starts_with"
| "ends_with";
value:
| string
| number
| boolean
| Array<string>
| Array<number>
| null;
}>;
}
| {
data: {
createdAt: number;
Expand Down Expand Up @@ -521,6 +558,7 @@ export declare const components: {
image?: string;
isAnonymous?: boolean;
name?: string;
stripeCustomerId?: string;
twoFactorEnabled?: boolean;
updatedAt?: number;
userId?: string;
Expand Down Expand Up @@ -566,6 +604,7 @@ export declare const components: {
image?: string;
isAnonymous?: boolean;
name?: string;
stripeCustomerId?: string;
twoFactorEnabled?: boolean;
updatedAt?: number;
userId?: string;
Expand Down Expand Up @@ -729,6 +768,42 @@ export declare const components: {
| null;
}>;
}
| {
model: "subscription";
update: {
cancelAtPeriodEnd?: boolean;
periodEnd?: number;
periodStart?: number;
plan?: string;
referenceId?: string;
seats?: number;
status?: string;
stripeCustomerId?: string;
stripeSubscriptionId?: string;
};
where?: Array<{
connector?: "AND" | "OR";
field: string;
operator?:
| "lt"
| "lte"
| "gt"
| "gte"
| "eq"
| "in"
| "ne"
| "contains"
| "starts_with"
| "ends_with";
value:
| string
| number
| boolean
| Array<string>
| Array<number>
| null;
}>;
}
| {
model: "jwks";
update: {
Expand Down
15 changes: 15 additions & 0 deletions examples/next/convex/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { convex } from "@convex-dev/better-auth/plugins";
import { api, components, internal } from "./_generated/api";
import { twoFactor } from "better-auth/plugins";
import { emailOTP } from "better-auth/plugins";
import { stripe } from "@better-auth/stripe";
import {
sendMagicLink,
sendOTPVerification,
Expand All @@ -19,6 +20,11 @@ import { betterAuth } from "better-auth";
import { GenericCtx, query } from "./_generated/server";
import { DataModel, Id } from "./_generated/dataModel";
import { asyncMap } from "convex-helpers";
import Stripe from "stripe";

const stripeClient = new Stripe(process.env.STRIPE_SECRET_KEY as string, {
apiVersion: "2025-05-28.basil",
});

const authFunctions: AuthFunctions = internal.auth;
const publicAuthFunctions: PublicAuthFunctions = api.auth;
Expand Down Expand Up @@ -89,6 +95,15 @@ export const createAuth = (ctx: GenericCtx) =>
},
}),
twoFactor(),
stripe({
stripeClient,
stripeWebhookSecret: process.env.STRIPE_WEBHOOK_SECRET as string,
createCustomerOnSignUp: true,
subscription: {
enabled: true,
plans: []
}
}),
convex(),
],
});
Expand Down
2 changes: 2 additions & 0 deletions examples/next/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"lint": "next lint && tsc -p convex && eslint convex"
},
"dependencies": {
"@better-auth/stripe": "^1.2.12",
"@convex-dev/better-auth": "file:../..",
"@radix-ui/react-checkbox": "^1.1.4",
"@radix-ui/react-dropdown-menu": "^2.1.6",
Expand All @@ -32,6 +33,7 @@
"resend": "^4.2.0",
"shadcn": "^2.4.0-canary.17",
"sonner": "^2.0.3",
"stripe": "^18.2.1",
"tailwind-merge": "^3.1.0",
"tw-animate-css": "^1.2.5"
},
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"type": "module",
"scripts": {
"build": "npm run build:esm && npm run build:cjs",
"build:esm": "tsc --project ./esm.json && tsc-alias -p ./esm.json && echo '{\\n \"type\": \"module\"\\n}' > dist/esm/package.json",
"build:cjs": "tsc --project ./commonjs.json && echo '{\\n \"type\": \"commonjs\"\\n}' > dist/commonjs/package.json",
"build:esm": "tsc --project ./esm.json && tsc-alias -p ./esm.json && echo {\"type\": \"module\"} > dist/esm/package.json",
"build:cjs": "tsc --project ./commonjs.json && echo {\"type\": \"commonjs\"} > dist/commonjs/package.json",
"typecheck": "tsc --noEmit",
"prepare": "npm run build",
"prepack": "node node10stubs.mjs",
Expand Down Expand Up @@ -146,7 +146,7 @@
"@edge-runtime/vm": "^5.0.0",
"@eslint/js": "^9.9.1",
"@tanstack/react-start": "^1.120.19",
"@types/node": "^18.17.0",
"@types/node": "^20.19.2",
"@types/react": "19.1.6",
"@types/react-dom": "19.1.6",
"concurrently": "^9.2.0",
Expand All @@ -164,6 +164,7 @@
"types": "./dist/commonjs/client/index.d.ts",
"module": "./dist/esm/client/index.js",
"dependencies": {
"@better-auth/stripe": "^1.2.12",
"@better-auth/utils": "^0.2.5",
"better-auth": "^1.2.12",
"better-call": "^1.0.9",
Expand Down
6 changes: 5 additions & 1 deletion src/client/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export const convexAdapter = (
return data;
},
},
adapter: ({ schema }) => {
adapter: () => {
return {
id: "convex",
create: async ({ model, data, select }): Promise<any> => {
Expand Down Expand Up @@ -179,6 +179,10 @@ export const convexAdapter = (
if (!("runMutation" in ctx)) {
throw new Error("ctx is not a mutation ctx");
}
if (data.where?.[0] && !data.where?.[0]?.operator) {
data.where![0].operator = "eq";
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The adapter should use eq as default, curious if this change is addressing a failure?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll have to check. I think the issue I was facing was because we weren't passing the "where" when getting a user, which would always return the oldest one, instead of the requested one.


if (data.where?.length === 1 && data.where[0].operator === "eq") {
const updateFn =
data.model === "user"
Expand Down
9 changes: 2 additions & 7 deletions src/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ export class BetterAuth<UserId extends string = string> {
throw new Error("invalid user");
}
const { id: _id, ...user } = doc;
return user;
return user as Infer<typeof schema.tables.user.validator>;
}

async getIdTokenCookieName(
Expand Down Expand Up @@ -213,12 +213,7 @@ export class BetterAuth<UserId extends string = string> {
handler: async (ctx, args) => {
const updatedUser = await ctx.runMutation(
this.component.lib.updateOne,
{
input: {
model: "user",
update: args.input.update,
},
}
args
);
// Type narrowing
if (!("emailVerified" in updatedUser)) {
Expand Down
75 changes: 75 additions & 0 deletions src/component/_generated/api.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export type Mounts = {
image?: string;
isAnonymous?: boolean;
name: string;
stripeCustomerId?: string;
twoFactorEnabled?: boolean;
updatedAt: number;
userId?: string;
Expand Down Expand Up @@ -218,6 +219,42 @@ export type Mounts = {
| null;
}>;
}
| {
data: {
cancelAtPeriodEnd?: boolean;
periodEnd?: number;
periodStart?: number;
plan: string;
referenceId: string;
seats?: number;
status?: string;
stripeCustomerId?: string;
stripeSubscriptionId?: string;
};
model: "subscription";
where?: Array<{
connector?: "AND" | "OR";
field: string;
operator?:
| "lt"
| "lte"
| "gt"
| "gte"
| "eq"
| "in"
| "ne"
| "contains"
| "starts_with"
| "ends_with";
value:
| string
| number
| boolean
| Array<string>
| Array<number>
| null;
}>;
}
| {
data: {
createdAt: number;
Expand Down Expand Up @@ -495,6 +532,7 @@ export type Mounts = {
image?: string;
isAnonymous?: boolean;
name?: string;
stripeCustomerId?: string;
twoFactorEnabled?: boolean;
updatedAt?: number;
userId?: string;
Expand Down Expand Up @@ -540,6 +578,7 @@ export type Mounts = {
image?: string;
isAnonymous?: boolean;
name?: string;
stripeCustomerId?: string;
twoFactorEnabled?: boolean;
updatedAt?: number;
userId?: string;
Expand Down Expand Up @@ -703,6 +742,42 @@ export type Mounts = {
| null;
}>;
}
| {
model: "subscription";
update: {
cancelAtPeriodEnd?: boolean;
periodEnd?: number;
periodStart?: number;
plan?: string;
referenceId?: string;
seats?: number;
status?: string;
stripeCustomerId?: string;
stripeSubscriptionId?: string;
};
where?: Array<{
connector?: "AND" | "OR";
field: string;
operator?:
| "lt"
| "lte"
| "gt"
| "gte"
| "eq"
| "in"
| "ne"
| "contains"
| "starts_with"
| "ends_with";
value:
| string
| number
| boolean
| Array<string>
| Array<number>
| null;
}>;
}
| {
model: "jwks";
update: {
Expand Down
Loading