init subscription (#51)

* init subscription

* add subscription plans

* fix docker

* fix docker

* fix docker

* wip

* wip
This commit is contained in:
Bill Yang 2025-03-11 21:03:23 -07:00 committed by GitHub
parent 2d22dc6fee
commit bcc1cc5d29
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
25 changed files with 1747 additions and 62 deletions

View file

@ -7,9 +7,13 @@ import { db } from "../db/postgres/postgres.js";
import { IS_CLOUD } from "./const.js";
import * as schema from "../db/postgres/schema.js";
import { eq } from "drizzle-orm";
import { stripe } from "@better-auth/stripe";
import Stripe from "stripe";
dotenv.config();
const stripeClient = new Stripe(process.env.STRIPE_SECRET_KEY!);
type AuthType = ReturnType<typeof betterAuth> | null;
const pluginList = IS_CLOUD
@ -21,6 +25,97 @@ const pluginList = IS_CLOUD
// Set the creator role to owner
creatorRole: "owner",
}),
stripe({
stripeClient,
stripeWebhookSecret: process.env.STRIPE_WEBHOOK_SECRET!,
createCustomerOnSignUp: true,
subscription: {
enabled: true,
plans: [
{
priceId: "price_1R1fIVDFVprnAny2yJtRRPBm",
name: "basic100k",
interval: "month",
limits: {
events: 100_000,
},
},
{
priceId: "price_1R1fKJDFVprnAny2mfiBjkAQ",
name: "basic250k",
interval: "month",
limits: {
events: 250_000,
},
},
{
name: "basic500k",
priceId: "price_1R1fQlDFVprnAny2WwNdiRgT",
interval: "month",
limits: {
events: 500_000,
},
},
{
name: "basic1m",
priceId: "price_1R1fR2DFVprnAny28tPEQAwh",
interval: "month",
limits: {
events: 1_000_000,
},
},
{
name: "basic2m",
priceId: "price_1R1fRMDFVprnAny24AMo0Vuu",
interval: "month",
limits: {
events: 2_000_000,
},
},
{
name: "pro100k",
priceId: "price_1R1fRmDFVprnAny27gL7XFCY",
interval: "month",
limits: {
events: 100_000,
},
},
{
name: "pro250k",
priceId: "price_1R1fSADFVprnAny2d7d4tXTs",
interval: "month",
limits: {
events: 250_000,
},
},
{
name: "pro500k",
priceId: "price_1R1fSkDFVprnAny2MzBvhPKs",
interval: "month",
limits: {
events: 500_000,
},
},
{
name: "pro1m",
priceId: "price_1R1fTMDFVprnAny2IdeB1bLV",
interval: "month",
limits: {
events: 1_000_000,
},
},
{
name: "pro2m",
priceId: "price_1R1fTXDFVprnAny2JBLVtkIU",
interval: "month",
limits: {
events: 2_000_000,
},
},
],
},
}),
]
: [
username(),
@ -53,7 +148,7 @@ export let auth: AuthType | null = betterAuth({
enabled: true,
},
},
plugins: pluginList,
plugins: pluginList as any,
trustedOrigins: ["http://localhost:3002"],
advanced: {
useSecureCookies: process.env.NODE_ENV === "production", // don't mark Secure in dev
@ -63,6 +158,7 @@ export let auth: AuthType | null = betterAuth({
},
},
});
export function initAuth(allowedOrigins: string[]) {
auth = betterAuth({
basePath: "/auth",
@ -132,7 +228,7 @@ export function initAuth(allowedOrigins: string[]) {
},
},
},
plugins: pluginList,
plugins: pluginList as any,
trustedOrigins: allowedOrigins,
advanced: {
useSecureCookies: process.env.NODE_ENV === "production",
@ -141,34 +237,5 @@ export function initAuth(allowedOrigins: string[]) {
path: "/",
},
},
// Use database hooks to create an organization after user signup
databaseHooks: {
user: {
create: {
after: async (user) => {
// Create an organization for the new user
console.info(user);
// if (auth) {
// try {
// const orgName = user.name || user.username || "My Organization";
// await auth.api.organization.createOrganization({
// body: {
// name: orgName,
// },
// headers: {
// "x-user-id": user.id,
// },
// });
// } catch (error) {
// console.error(
// "Error creating organization for new user:",
// error
// );
// }
// }
},
},
},
},
});
}